What does this mean? (Event subclasses)

14 posts

Flag Post

I don’t understand this sentence from Essential AS3:

“Note that every custom Event subclass must override both clone() and toString(), providing versions of those methods that account for any custom variables in the subclass (e.g. isOn).”


Also, I’m confused about the following line of code. What exactly is going on here?

dispatchEvent(new ToggleEvent(ToggleEvent.TOGGLE, true, false, isOn));

Thanks!

 
Flag Post
public class ToggleEvent extends Event {

ToggleEvent is a subclass of Event. (aka, extends)

Some extra information that is super cool. Flash has already created a ton of Event subclasses for you, that handle a ton of different things. See:


dispatchEvent(new ToggleEvent(ToggleEvent.TOGGLE, true, false, isOn));

So while you are familiar with listening for events, this is a way to actually create (dispatch) them. dispatchEvent allows your instance to broadcast an event of your choice. In the example, it is dispatching a ToggleEvent. By logical reasoning, I would guess the following:

Event’s constructor has


Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)

So, I’m assuming that the ToggleEvent.TOGGLE, true, false are for these paramaters. Thus, the , isOn is likely taken care of in the ToggleEvent class.

 
Flag Post
Originally posted by GameBuilder15:

I don’t understand this sentence from Essential AS3:

“Note that every custom Event subclass must override both clone() and toString(), providing versions of those methods that account for any custom variables in the subclass (e.g. isOn).”

If you are used to clone creatures, say jellyfish, you can encounter problems when you clone Humans the same way. If nobody told you that a Human needs a brain for example, you could accidentally forget it, because you’re not used to cloning things with a brain.

To make other people aware of how to clone your own creations, you should provide a method for that, which takes care of all the special features you added.

toString() is pretty similar, every of your classes should override it to use it properly.
If you want a Human, you want to get information of the Human, not some other superclass.

Why not post in this thread with pretty much the same topic?
http://www.kongregate.com/forums/4-game-programming/topics/344017-what-is-an-event-object

 
Flag Post

Okay get this:
Say you want to create your own Event class so you can do stuff like this:
player.addEventListener(GameEvent.DEAD, playerDead);

Well, if you do just that, nothing will happen because GameEvent.DEAD is not built into Flash Player. You need to dispatch an event:

// when the player is presumed dead
dispatchEvent(new GameEvent(GameEvent.DEAD));

In the dispatchEvent you posted, the ToggleEvent class has 4 constructors that you can specify when you do new ToggleEvent(). This allows you to do neat things like send the time the player died also:

dispatchEvent(new GameEvent(GameEvent.DEAD, playerTime));

If you store this in your event class like this:

public var pTime:Number;
public function GameEvent(type:String, playerTime:Number):void
{
    super(type);
    pTime = playerTime;
}

'pTime' can later be accessed through the Event/GameEvent variable in the callback for addEventListener:
public function playerDead(eventObj:GameEvent):void
{
    trace(eventObj.pTime); 
}
 
Flag Post

I personally disagree with the author on this point, it is not an absolute necessity, just best practice. I used to create loads of custom events in my games and have not once overridden the clone() or toString() methods.

 
Flag Post

Thanks, everyone. I think I get it now.

This is probably a really dumb question, but RTL, what’s the following code for?

super(type);

 
Flag Post

super gives the parent class(Event) parameters in the constructor, which is the case of type:String

You can also use super.toString() this would ignore your overridden toString() function and use the parent default one, so super.my_function to access those overridden functions

 
Flag Post

So basically it passes the type:String parameter to the Event class’s constructor?

 
Flag Post

yep, you must have it every time the parent of a subclass needs parameters, you can only have it once and you mustn’t use variables/functions of the parent class before calling the super(), it tends to break stuff

 
Flag Post

Of course. Got it. Thanks. I guess I just didn’t realize the Event class needed the type parameter.

 
Flag Post

I have another (probably dumb) question:

public override function clone():Event{

What’s “Event” doing there? Is it because clone is part of the Event class? I’m confused because normally that’s where “void” goes.

 
Flag Post

Not a AS programmer, but assuming it is similar to other languages which it has time and time again you would be right, it is because it is part of Event

 
Flag Post

Thats the return type. Void means that nothing is returned. In this case clone is in the Event class and is thus returning a clone of the Event, therefore return type is Event.

 
Flag Post

That explains why AS code seems so weird to me, lol