I am new to this and am trying to figure out how to dispatch events.
I want to dispatch an event on click of my buttons and have the following. What am I doing wrong? What is this not working?
Thank you very much for any help with this!
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class HomeMain extends MovieClip
{
//Button Variables
private var home_btn0:MovieClip;
private var home_btn1:MovieClip;
private var numOfBtns:Number = 2;
// Event Dispatcher
public var dispatchEvent:Function;
public var addEventListener:Function;
public function HomeMain(Void)
{
//Activate the buttons
EventDispatcher.initialize(this);
activateBtns();
}
private function activateBtns():Void
{
//Activate the buttons
for(var i:Number = 0; i < numOfBtns; i++)
{
var ref:MovieClip = this["home_btn" + i];
ref.id = i;
//Btn actions
ref.onRelease = function():Void {
trace(this.id);
dispatchEvent({type:"itemClicked"});
}
}
home_btn0.addEventListener("itemClicked", Delegate.create(this, onItemClick));
home_btn1.addEventListener("itemClicked", Delegate.create(this, onItemClick));
}
private function onItemClick(e:Object):Void
{
trace("I hope I get this to work");
}
}