i am a bit embarassed to ask this, but i do this so much i have to ask. Occasionally, i need to pass/handle custom events on the fly. Rather than creating a whole custom event class, i will just do something like
Code:
myObject.dispatchEvent(new Event('thisCustomEvent'));
//and then handle it later
myObject.addEventListener('thisCustomEvent',doSomething);
but what if i want to pass some more info with that custom event...do i have to make it a public variable of myObject? For example, if myObject is storing a number and i want to fire off with the event, and only pass that variable, is there a way to do something like:
Event does not have any payload property that you can pass along with it, but you could create a subclass which has an arbitrary content property. There would be no need to make that extra value a public property in any class other than the event.
Code:
public class DataEvent extends Event{
public var content:*;
public function DataEvent(type:String, data:*, bubbles:Boolean = false, cancelable:Boolean = false){
super(type, bubbles, cancelable);
content = data;
}
public function clone():Event{
return new DataEvent(type, content, bubbles, cancelable);
}
}
You can re-use that DataEvent in many projects. Your example might look like:
What I usually do when using generic events is store the value needed in a property of the dispatcher then cast event.target to the appropriate class to retrieve the value. Obviously this doens't cover all situations but generally gets the job done simply.
And if you're too lazy to make a new Event class but want to have events with custom properties, you can also use a SyncEvent http://livedocs.adobe.com/flex/3/lan...SyncEvent.html
with its changeList array, you can throw assign anything you want to it (well, to the array, not the event object directly).
so, in the end , if i want to pass a payload i have no choice but to take a moment to write a subclass....ok....it's what i thought - time to break out the editor
And if you're too lazy to make a new Event class but want to have events with custom properties, you can also use a SyncEvent http://livedocs.adobe.com/flex/3/lan...SyncEvent.html
with its changeList array, you can throw assign anything you want to it (well, to the array, not the event object directly).
var MyClass:theClass = new theClass(); MyClass.addEventListener("callBack",showMessage); function showMessage(de:DataEvent):void{ trace("doSomething triggered. data is: "+de.content); }
Your dispatching class is called test, but you've added the listener to an instance of theClass. That means that the dispatching instance is not the listening instance. Since you did not pass a value to the DataEvent constructor for bubbles, the DataEvent will not bubble up through the display list.
What is the relationship between your instance of test and your instance of theClass?
Oh, well in that case the event is dispatched before you add the listener. Don't dispatch events in a constructor. I can't think of a good reason for it*. Nothing outside that instance could possibly hear it, and anything inside that instance, you could just call the function directly.
*actually, I can think of one design in which that makes a bit of sense, but I do not want to encourage it to beginners.
AS3 has taken a lot of the fun out of making flash games. It used to be just about "hacking" something together -- and it "just worked".
Now you have to know the "correct" way to do something - just to get the game to run.
I've been working with AS2 for years - and have never heard of a constructor! LOL. Yes, i'm sure my code is terrible - but it used to be great just to make a game, without thinking if i was doing it right.
Sorry for all the questions 5TonsOfFlax -- please would you mind looking at my code and letting me know if what i have done is ok.
The reason i have done it like this is -- i can make lots of games just by changing the fla file -- and i never have to change the classes.
I can add as many or change names of movieclips that the class can access -- without passing the whole stage to the class.
What do you think? Is that ok? is it ok to call a dispatchEvent every 10 in a timer?
I looked at the .as files in your zip and they look okay, but I cannot open the fla because I don't have Flash. The code which actually creates an instance of theClass is in the fla, if anywhere, so I can't verify that that is correct.
If it's the same as the code you posted above, then it should work. You should get about 100 traces of "doSomething triggered. data is: ". I'm not sure if the dataEvent content will be the same i variable in each of those, so you may get repeats. It depends on whether the flash player passes int primitives by reference or value, and the timing of execution.
What results are you getting?
I certainly wouldn't have done it this way, but I don't even really know what "it" you're trying to accomplish.
As for AS2 "just working", that is the opposite of my experience. Hardly anything in AS2 worked the way I thought it should, and the necessity of using animation-oriented features (timelines!) to do programming tasks was extremely irritating.
Your reasons for "doing it like this" don't make sense to me. I literally do not know what you are trying to say there. But if you mean that you can change visual appearances without changing the backing behavior defining classes, you could still do that with a thoughtfully designed architecture.