Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
ohh thank you
can you please tell me how can i add parent to this class from the begin (i want the main stage to be its parent) or can you fix it please if you dont mind
thank you
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
onAdded get's called after addChild so what you have written makes no sense.
if MAINMAP is you document class then you don't need to call addChild that will happen automatically
Copy link to clipboard
Copied
yeah i know that brother
but i want it to be in the same class inside because i have lvl 1 and 2 and 3
can you please fix that code to make the main its parent
thank you alot brother For your patience
Copy link to clipboard
Copied
Sorry I don't understand what you are after
Copy link to clipboard
Copied
which thing you dont get it?
Copy link to clipboard
Copied
what you are trying to achieve
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
you can't
you can never call MovieCli(parent).addChild(this) / as I am sure you understand now you don't have access to parent until after calling addChild()
ok let's try and take this step by step
do you have a document class? if so please paste it here
Copy link to clipboard
Copied
no i dont have only 1 class in the library
Copy link to clipboard
Copied
so you are adding you actionscript on the timeline?
Copy link to clipboard
Copied
no its out in the folder
Copy link to clipboard
Copied
Yes MAINMAP1 might be an external class, but you need to to either have a document class or add code the timeline as well
Copy link to clipboard
Copied
ohh so there is no solutin except this right?
Copy link to clipboard
Copied
a class can't add itself to the display list. you have access to the display list from a document class or the timeline. you can then add any other objects to the display list including your MAINMAP1
Copy link to clipboard
Copied
oh thank you alot brother , i get it now all