0
votes

At the beginning my stage is empty, I've an empty array and some global variables, and I have two movie clips in my library: one is a trigger, second is the movie clip that is placed on the stage when the trigger is activated (a testdummy, if you want).

var dummies:Array = [];
var i:int = 300;
var j:int = 500;
var k:int = 0;

Then I place an instance of the trigger on the stage

var testaddbutton:AdditionButton = new AdditionButton();
addChild(testaddbutton);
testaddbutton.x = i;
testaddbutton.y = i;

Then I describe the behavior of the trigger movie clip. When it is clicked, a new instance of dummy clip is placed on stage, is inserted into array, and finally certain dynamic text fields within the created instance are assigned certain values (not the point really)

testaddbutton.addEventListener(MouseEvent.CLICK, placedummy);

function placedummy(evt:MouseEvent):void {
    var dummy:Dummy = new Dummy();
    addChild(dummy);
    dummies.push(dummy);
    dummies[k].x=i + l*100;
    dummies[k].y=j;
    k++;
    dummy.dummyindex.text = k.toString();
}

But then I face a problem. I want the dummies to execute a function when they are clicked. When I try to add an eventListener to dummy...

//dummy.addEventListener(MouseEvent.CLICK, showlifesigns);
//function showlifesigns(evt:MouseEvent):void {
//    trace ("pew");
//}

...I get an obvious error : Scene 1, Layer 'actions', Frame 1, Line 66 1120: Access of undefined property dummy. Because dummies are created within a function. I don't have any trouble addressing items in the 'dummies' array though.

Though dummies are crated by the same pattern, data stored in them is the whole point of creating the application.

So the question is: how can I apply a function to each of those dummies, which is executed on click and affects only one dummy?

Thanks in advance

2
Is this your actual listener code? The error says userevent not found, and there is no userevent in the code you posted. - taskinoor
jee, sorry, I edited the names before posting, forgot to fix that one. - Anton Matyulkov

2 Answers

1
votes

Just call addEventListener while dummy is still a valid reference -- in the same function you use to you create each dummy item:

function placedummy(evt:MouseEvent):void { 
    var dummy:Dummy = new Dummy(); 
    addChild(dummy); 

    // Add the event listener right here!
    dummy.addEventListener(MouseEvent.CLICK, showlifesigns); 

    dummies.push(dummy); 
    dummies[k].x=i + l*100; 
    dummies[k].y=j; 
    k++; 
    dummy.dummyindex.text = k.toString(); 
} 
0
votes

The scope of variable dummy is the placedummy function. So you can not refer this outside of this function. You need to move the listener adding code within the function.

function placedummy(evt:MouseEvent):void {
    // other stuffs 
    dummy.addEventListener(MouseEvent.CLICK, showlifesigns);
}

Now all created dummy will listen for the event. In the event listener showlifesigns you can use evt.target to get the particular dummy which is clicked.

function showlifesigns(evt:MouseEvent):void {
    var clickedDummy:Dummy = evt.target as Dummy;
    // do stuff with clickedDummy
}