hi how are you guys ?i hope you all fine .
iam trying to understand how add and removing child work and i did this short code
here how its work
1-the main stage is empty nothing in it
2- in the library there is class called (MAINMAP1) the only class in the project
3- i did 2 codes in the (MAINMAP1) class , the first didn.t work and the other work.
4- iam running from the main stage.
here
the first code
package
{
import flash.system.System;
import flash.system.fscommand;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class MAINMAP1 extends MovieClip {
public function MAINMAP1(){
MovieClip(parent).addChild(this);
}
}
}
it doesn.t add any thing in the main stage it still empty
but the other work (when i added instance of the map class to the main stage this code remove it)
package
{
import flash.system.System;
import flash.system.fscommand;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class MAINMAP1 extends MovieClip {
public function MAINMAP1(){
MovieClip(parent).removeChild(this);
}
}
}
thats all
i hope you guys can help me
thank you
in the first example you gave
when you construct MAINMAP1 it tries to add itself to its parent.
but it's doesn't have a parent yet cause you haven't added it to anything
in the second example you manually add MAINMAP1 to the stage (it now has a parent)
so when you construct MAINMAP it removes itself from its parent
to be honest I am suprised even the second one worked
either add MAINMAP1 using addChild in the constructor of your document class (if you have one)
or you'll have to add it on the time line
you should add an eventlistener in the constructor of MAINMAP
addEventListener(Event.ADDED_TO_STAGE, onAdded);
and then in your function onAdded you can access parent
thanks
the first way i did it in the first game but here i wanna make game with only 1 class , i will link it later
the second i did it in the same class like this
package
{
import flash.system.System;
import flash.system.fscommand;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class MAINMAP1 extends MovieClip {
public function MAINMAP1(){
addEventListener(Event.ADDED_TO_STAGE, onAdded)
}
public function onAdded (event:Event):void {
MovieClip(parent).addChild(this);
}
}
}
the problem still
maybe i forget something?
thank you
i just want to add some code to this code to let it work from MAINMAP class
package
{
import flash.system.System;
import flash.system.fscommand;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class MAINMAP1 extends MovieClip {
public function MAINMAP1(){
MovieClip(parent).addChild(this);
}
}
}
can you please do that?
some codes to let it work well in this code
North America
Europe, Middle East and Africa
Asia Pacific