PDA

View Full Version : event target issue


madcat
10-31-2008, 01:30 PM
Hopefully this is easy... I'm still learning.

I am creating a series of dynamic sprites that contain thumbnail images. When you click on one, I need the name 'thumbX' to pop-up so I can use them for other events in other functions. But now with trace(event.target); I only get 'object Thumb1'.

So I'm referencing the class -- pretty much any event.whatever code I try won't give me 'thumb1', or 'thumb2', etc.

Abbreviated code...


// at top of class
private var thumb1:Thumb1 = new Thumb1();
private var thumb2:Thumb2 = new Thumb2();
private var thumb3:Thumb3 = new Thumb3();

thumbs = [thumb1, thumb2, thumb3];



// In a function I create a series of myBox sprites, and add the thumb array to the sprite container
for (var i = 0....
addChild(myBox);
myBox.addChild(thumbs[i]);
myBoxes.push(myBox);


How would I get 'thumbX' with my code? I think all of the relevant info is above.

Thanks for any help on this.

Slowburn
10-31-2008, 02:08 PM
you don't actually need to get 'thumbX' as the 'event.target' gives you a reference to the object clicked. <- it's the same object



// first establish a reference to your instance object
var instance1:ClassType = new ClassType();
// give your object a name
instance1.name = "instanceName1";
// add event handlers for interaction
instance1.addEventListener( MouseEvent.CLICK, handleClick, false, 0, true );
// add the object to the display-list
addChild( instance1 );

function handleClick( event:MouseEvent ):void
{
trace( "instance reference:", event.target ); // [object ClassType]
trace( "instance name:", event.target.name ); // instanceName1
// above and below are the same object
trace( "actual instance ref.:", instance1 ); // [object ClassType]
trace( "actual instance name:", instance1.name ); // instanceName1
}