Adding child to the stage using action script


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import flash.events.Event;
  2.  
  3. import flash.events.MouseEvent;
  4.  
  5. /*Below events allow the mouse move event only when mouse is down and stop the move event when mouse is up*/
  6.  
  7. stage.addEventListener(MouseEvent.MOUSE_DOWN,down)
  8. function down(event:MouseEvent){
  9. stage.addEventListener(MouseEvent.MOUSE_MOVE,generate)
  10. }
  11. stage.addEventListener(MouseEvent.MOUSE_UP,up)
  12. function up(event:MouseEvent){
  13. stage.removeEventListener(MouseEvent.MOUSE_MOVE,generate)
  14. }
  15.  
  16. //****************************************************
  17. /*Actual logic to generate childs, myDot is the instance name of the class Dot*/
  18.  
  19. /*stage.addEventListener(MouseEvent.MOUSE_MOVE,generate)*/
  20. function generate(event:MouseEvent){
  21. var myDot:Dot=new Dot()
  22. addChild(myDot) /*Adding child to stage*/
  23. myDot.x=mouseX
  24. myDot.y=mouseY
  25.  
  26. /*To generate random size of the object or child */
  27. myDot.scaleX=myDot.scaleY=Math.random()+.2
  28. /*To generate the random frame numbers */
  29. myDot.gotoAndStop(Math.round(myDot.totalFrames*Math.random())+1)
  30. /*To generate random numbers of range equal to the size of stage */
  31. myDot.dx=Math.round(Math.random()*800)
  32. myDot.dy=Math.round(Math.random()*600)
  33.  
  34. myDot.addEventListener(Event.ENTER_FRAME,update)
  35. }
  36.  
  37. /*Here Function update is an ENTER_FRAME event.This function is executed every mili second*/
  38.  
  39. function update(event:Event){
  40. event.target.x+=(event.target.dx-event.target.x)/10
  41. event.target.y+=(event.target.dy-event.target.y)/10
  42.  
  43. /*To decrease the alpha value of the childs */
  44.  
  45. if(Math.round(event.target.x)==Math.round(event.target.dx)){
  46. if(event.target.alpha>0){
  47. event.target.alpha-=0.5
  48. }
  49. }
  50. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.