0
votes

Okay so here is a question that is more about method i think. I am fairly new at this and i therefore think a have the wrong approach.

I want to make a "game" where every time you click, the stage creates an addChild. After searching trough several sites, with my limited knowledge and trying back and forth, i have reached this conclusion:

mouse.addEventListener(MouseEvent.CLICK, CREATE);
function CREATE(e:Event):void {
    var square:MovieClip = new squarelink();
    addChild(square);
    square.x = mouseX;
    square.y = mouseY;
} 

Now my problem is that i want to code it, give this new movieclip some "events" but because my movieclip do not appear on stage until after i click. Any addeventlistener in its name is going to come up as: access of possibly undefined property. This causing my swf to fail.

So therefor i tried to set the "var square:MovieClip = new squarelink();" out of my CREATE function. This just causing it to only move the square about as i clicked, not create new once. How ever made it possible for me to now add events.

So to sum up. I need a method that allows me to create several "children" as well as letting me add an event that states, if i click on it again: Delete it self.

This without it telling me that there is not yet any square on stage.

(This is an explanation of the problem, any comment on spelling mistake, or "you need a big R in that one for it to work" is welcome, but likely unusable. I know how to write the code properly, i just do not know which code do write.)

I hope that this problem is some what well explained, and i hope someone in here can help, as there is 1 month till i am back in school to ask my teacher. :D

3

3 Answers

1
votes

You should develop a more complicated, object oriented solution to your problem. Have a main application class to create objects and have those created objects dispatch "please remove me" event's to the creator object.

However, considering this simple example you gave here, there's a simple solution involving stopping event propagation to the stage from the clicked object.

stage.addEventListener(MouseEvent.CLICK, createCircle);
function createCircle(event:MouseEvent):void
{
    var circle:Sprite = new Sprite();
    circle.graphics.beginFill(0x00FF00);
    circle.graphics.drawCircle(0, 0, 20);
    circle.x = stage.mouseX;
    circle.y = stage.mouseY;
    stage.addChild(circle);

    circle.addEventListener(MouseEvent.CLICK, removeCircle);
}

function removeCircle(event:MouseEvent):void
{
    stage.removeChild(event.currentTarget as DisplayObject);

    // magic!
    event.stopPropagation();
}
0
votes

The above code is for creating a movieclip instance on the stage. To remove the movie clip, you need to attach a class with that movieclip. In that class, add an event listener which has to delete the contained movieclip.

0
votes

addChild PARADOX ?? Anyways, all I understood is that you need to:

  • Add movieclips onto stage on stage click.
  • Remove movieclip when clicked once & then clicked again.

If that is the case, use this as an example:

stop();

stage.addEventListener(MouseEvent.CLICK, createBox);

function createBox(e:MouseEvent) {

    if(e.target != stage) return;

    var square:MovieClip = new MovieClip();

    square.graphics.beginFill(0,1);
    square.graphics.drawRect(0,0,25,25);
    square.graphics.endFill();

    square.x = 100;
    square.y = 100; 
    square.name = "mc" + this.numChildren;

    square.addEventListener(MouseEvent.CLICK, function(e) {

        var sq:MovieClip = e.currentTarget;

        if(sq.name.charAt(0) == '_') removeChild(sq);

        else sq.name = "_" + sq.name;       
    });

    addChild(square);   
}