Reparenting Sprites


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



Copy this code and paste it in your HTML
  1. package{
  2. import flash.display.Sprite;
  3. import flash.events.MouseEvent;
  4.  
  5. [SWF(backgroundColor="0x333333", width="800",
  6. height="600", frameRate="31")]
  7.  
  8. public class Reparenting extends Sprite {
  9. private var parent1:Sprite;
  10. private var parent2:Sprite;
  11. private var ball:Sprite;
  12.  
  13. public function Reparenting(){
  14. init();
  15. }
  16.  
  17. private function init():void {
  18. parent1 = new Sprite();
  19. addChild(parent1);
  20. parent1.graphics.lineStyle(1,0);
  21. parent1.graphics.drawRect(-50,-50,100,100);
  22. parent1.x = 60;
  23. parent1.y = 60;
  24.  
  25. parent2 = new Sprite();
  26. addChild(parent2);
  27. parent2.graphics.lineStyle(1,0);
  28. parent2.graphics.drawRect(-50,-50,100,100);
  29. parent2.x = 170;
  30. parent2.y = 60;
  31.  
  32. ball = new Sprite();
  33. parent1.addChild(ball);
  34. ball.graphics.beginFill(0xffffff);
  35. ball.graphics.drawCircle(0,0,40);
  36. ball.graphics.endFill();
  37. ball.addEventListener(MouseEvent.CLICK, onBallClick);
  38. }
  39.  
  40. public function onBallClick(event:MouseEvent):void {
  41. parent2.addChild(ball);
  42. }
  43.  
  44. }//class
  45. }//package

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.