既にaddChildされてるMCを別のMCにaddChildすると前のからremoveされる確認。 黒い四角をクリックするとその中にtargetMC(赤い円)をaddChildする。 その際、targetMCが既に別のMC上にaddChildされている場合は、 自動的にremoveされてから,addChildすることを確認。 024t910 forked:2favorite:0lines:46license : All rights reserved modified : 2009-04-15 19:11:31 Embed Tweet package { /* 黒い四角をクリックするとその中にtargetMC(赤い円)をaddChildする。 その際、targetMCが既に別のMC上にaddChildされている場合は、 自動的にremoveされてから,addChildすることを確認。 */ import flash.display.Sprite; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; public class AddChildTest extends Sprite { private var targetMC:MovieClip = new MovieClip(); private var _txt:TextField = new TextField(); public function AddChildTest () { _txt.height = stage.stageHeight; addChild(_txt); targetMC.graphics.beginFill(0xFF00000); targetMC.graphics.drawCircle(0,0,50); targetMC.graphics.endFill(); //addChild(targetMC); var mc1:MovieClip = ContainerMCFactory(); mc1.y = 300; mc1.x = 100; addChild(mc1); mc1.addChil var mc2:MovieClip = ContainerMCFactory(); mc2.y = 300; mc2.x = 300 addChild(mc2); targetMC.addEventListener(Event.ADDED,onAdded); targetMC.addEventListener(Event.REMOVED,onRemoved); mc1.addEventListener(MouseEvent.CLICK,addChildTarget); mc2.addEventListener(MouseEvent.CLICK,addChildTarget); } private function ContainerMCFactory():MovieClip{ var mc:MovieClip = new MovieClip(); mc.graphics.beginFill(0); mc.graphics.drawRect(0,0,100,100); mc.graphics.endFill(); return mc; } private function onAdded(e:Event):void{ _txt.appendText("onAdded\n"); } private function onRemoved(e:Event):void{ _txt.appendText("onRemoved\n"); } private function addChildTarget(e:MouseEvent):void{ e.target.addChild(targetMC); } } } Code Fullscreen Preview Fullscreen MovieClip MouseEvent.ADDED MouseEvent.REMOVED appendText MouseEvent.CLICK addChild height addEventListener MouseEvent TextField sort new page view favorite forked pv305 forked from: 既にaddChildされてるMCを.. youyokoi forked:0 favorite:0lines:46 (diff:6) pv315 forked from: 既にaddChildされてるMCを.. hacker_f4482ykb forked:0 favorite:0lines:46 (diff:1)