PDA

View Full Version : Adding event listeners to dynamically created instances


sldhana
10-16-2005, 05:09 PM
I have a Guest class like this:-

class Guest{
private var guest_mc:MovieClip;
private var guestName: String;
private var rfid:Number;


public function Guest(name:String, target:MovieClip, depth:Number){
guest_mc = target.attachMovie("guest", name, depth);
guest_mc.addMouseListener('mouseMove', this);
}

public function setPosition(x:Number, y:Number){
guest_mc._x = x;
guest_mc._y = y;
}

public function setGuestName(guestName:String){
this.guestName = guestName;
}

public function getGuestName ():String{
return guestName;
}

public function setRfid(rfid:Number){
this.rfid = rfid;
}

public function getRfid ():Number{
return rfid;
}


public function mouseMove ():Void {
trace("Moved the Mouse");
}

}

Since I would have many instances of Guest created dynamically, I am iterating througha for loop to make life easier

var numOfPeople = names.length;
for(var i=0; i<numOfPeople; i++){
var someguest="guestclip" + i;
var x = random(300);
var y = random(300);
var statusNum = charStatus[i];
if(statusNum == "1"){
this[someguest] = new Guest("guest"+i, this, i)
this[someguest].setPosition(x,y);
this[someguest].setGuestName(names[i]);
}


When a guest instance is clicked, I would like it to identify itself, that is perhaps return the guestName that I set using the OOP technique. The big question is- why doesnt the dynamic movie clip respond to the mouse click although I have a MouseListener added to it? I want to add the mouse listener in the class definition since it would be easier to handle events for multiple instances(i think so, others might think otherwise).