PDA

View Full Version : Newbie AS 3 Question (Capturing Menu Click/How to call back from class event listene)



mwiley63
August 8th, 2007, 08:42 PM
Hello,

I have recently started learning actionscript 3, and I think this is just some concept I am not understanding.

I found a class for a menu that I am using, basically I want to attach a Movie Clip to the stage when a menu button is hit, but I am stuck on how to do this. Basically I have this in my main timeline:

import menuexample.BasicMenu;
import movieClipTest.movieClipTest;
var myMenu:BasicMenu = new BasicMenu();
myMenu.init();
myMenu.x = 50;
myMenu.y = 100;
addChild(myMenu);

var myClip:movieClipTest = new movieClipTest();
myClip.init();

// now how do I add it to the stage when a button is hit in the menu?


I have this in my BasicMenu class:

private function displayMessage(event:MouseEvent):void {

// Output the name of the clicked button.
trace(event.currentTarget.name);
}

Which traces the text (which is set to the button name) to the output window.

Can I do any kind of call back function? I know AS3 is trying to get away from this.

Thanks in advandce

mwiley63
August 9th, 2007, 12:49 AM
Never mind, I figured it out. I just set a click event listener and used a public variable inside of the class to get the value of the button that was pressed. I don't know why I didn't think of that before.

tiboO
August 9th, 2007, 05:58 AM
actually, if you set an event listener, the listening function will receive an Event paramater, which can be used to point the target object. In the case of a MouseEvent listening class, you can do something like this :


anObject.addEventListener(MouseEvent.CLICK,onMouse Click,false,0,true);

//listening function :
public function onMouseClick(e:MouseEvent)
{
e.target.alpha=.5; // for example, you could do anything involving e.target
}