Skip navigation
thunderxlight11111
Currently Being Moderated

adding and removing child

Apr 19, 2012 7:58 AM

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

 
Replies
  • Currently Being Moderated
    Apr 19, 2012 8:44 AM   in reply to thunderxlight11111

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 1:15 AM   in reply to thunderxlight11111

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 3:50 AM   in reply to thunderxlight11111

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 4:57 AM   in reply to thunderxlight11111

    Sorry I don't understand what you are after

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 5:00 AM   in reply to thunderxlight11111

    what you are trying to achieve

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 5:42 AM   in reply to thunderxlight11111

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 5:45 AM   in reply to thunderxlight11111

    so you are adding you actionscript on the timeline?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 5:58 AM   in reply to thunderxlight11111

    Yes MAINMAP1 might be an external class, but you need to to either have a document class or add code the timeline as well

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 20, 2012 6:22 AM   in reply to thunderxlight11111

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points