0

How can I add a children (instance) on stage every time I press the button without replacing the existing children?

I have four Classes: Symbol1, Symbol3, Symbol4, all.

When I Press Symbol3 which is a button I want to create an instance of Symbol1 on the stage through class all.as. With Symbol4 I want to delete one of the created instance in order of creation on stage. Example: I have pressed Symbol3 three times and I have created three instances of Symbol1 on stage. Now if I press Symbol4 I will delete the first created instance. If I press Symbol4 one more time I will delete the second created instance.

    public class Symbol3 extends SimpleButton
{
    private var creator:all;
    private var child:Symbol1 = new Symbol1  ;
    private var child2:Symbol1 = new Symbol1  ;
    private var child3:Symbol222 = new Symbol222  ;

    public function Symbol3()
    {
        addEventListener(MouseEvent.CLICK, onCLICK);
    }

    private function onCLICK(s:MouseEvent)
    {
        creator = new all(child);
        stage.addChild(creator);
    }
}

.

    public class all extends MovieClip
{
    private var _thief1:MovieClip;

    public function all(par1:MovieClip)
    {
        _thief1 = par1;
        addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
    }

    private function onADDED_TO_STAGE(e:Event)
    {
        removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
        this.addChild(_thief1);
        _thief1.x = Math.random() * 200;
        _thief1.y = Math.random() * 200;
    }
}

.

    public class Symbol4 extends SimpleButton
{
    public function Symbol4()
    {
        addEventListener(MouseEvent.CLICK, onCLICK);
    }

    private function onCLICK(s:MouseEvent)
    {
        stage.removeChild(?);
    }
}

This I have so far. Thanks

irnik
  • 139
  • 1
  • 2
  • 13
  • Try returning the value creator, then call to it(stage.removeChild(creator)) and import the file. –  Apr 23 '13 at 23:31
  • I'd encourage you to use variable names that are meaningful, as `Symbol3` and `Symbol4` are rather vague and make your code cryptic. How about `CreateButton` ? `DeleteButton` ? `all` is also a vague choice for a class name. – prototypical Apr 23 '13 at 23:41
  • Thanks for your answer. This is just an example I made quickly. I will appreciate if you have some ideas regarding the logic. Thanks – irnik Apr 23 '13 at 23:59

2 Answers2

0

You should put all your addable/removable sprite in one same container, let's call it container. Then the add button will look like this:

private function onCLICK(s:MouseEvent)
{
    container.addChild(new all(new Symbol1()));
}

And the remove button:

private function onCLICK(s:MouseEvent)
{
    container.removeChildAt(0);
}

When removing the child on layer 0, the other children will go one layer down and the next child to remove will come on 0.

Kodiak
  • 5,978
  • 17
  • 35
  • Thank you for this answer. I got your point but I still cannot create another child without replacing the existing one!? Every time I press the button to create a child the existing one disappear. – irnik Apr 24 '13 at 13:26
  • I basically want every time the button is pressed new child to appear on stage. – irnik Apr 24 '13 at 13:27
  • The problem is that you don't reinstantiate the "child" MovieClip (then called par1, then called _thief1). I edited my answer. – Kodiak Apr 24 '13 at 13:35
0

Thanks for the help Kodiak!

I made it finally. I am not sure if this is the right approach but at least it works.

I have three Classes: AddChild2.as - linkage to Button Creator.as Ship2.as - linkage to MovieClip

The tricky moment was that the stage had to be transferred as a parameter to avoid error:1009. The other think is the empty constructor function of the Creator that makes the code more flexible and independent. Now Creator can produce any passed movieClip. Again I believe that there is another better way to do this, so any improvement is welcome.


    public class AddChild2 extends SimpleButton
{
    private var creatorche:Creator = new Creator;
    private var s:Ship2;

    public function AddChild2()
    {
        // constructor code
        addEventListener(MouseEvent.CLICK, onCLICK)
    }

    private function onCLICK(e:MouseEvent)
    {
        s = new Ship2;
        creatorche.onCreator(s, stage);
    }
}

.

    public class Creator extends MovieClip
{
    private var ship:MovieClip;

    public function Creator()
    {
        // constructor code
    }

    public function onCreator(par1:MovieClip, par2:Stage)
    {
        ship = par1;
        par2.addChild(ship);
        ship.x = Math.random() * 200;
        ship.y = Math.random() * 200;
    }

}

.

    public class Ship2 extends MovieClip
{


    public function Ship2()
    {
        // constructor code
    }
}
irnik
  • 139
  • 1
  • 2
  • 13