starling-handbook-preview

ggschelling

Custom Events

Dogs bark for different reasons, right? Einstein might indicate that he wants to pee, or that he is

hungry. It might also be a way to tell a cat that it’s high time to make an exit.

Dog people will probably hear the difference (I’m a cat person; I won’t). That’s because smart dogs

set up a BarkEvent that stores their intent.

public class BarkEvent extends Event

{

public static const BARK:String; 1

private var _reason:String; 2

public function BarkEvent(type:String, reason:String, bubbles:Boolean=false)

{

super(type, bubbles); 3

_reason = reason;

}

}

public function get reason():Boolean { return _reason; } 4

1 It’s a good practice to store the event type right at the custom event class.

2 The reason for creating a custom event: we want to store some information with it. Here, that’s

the reason String.

3 Call the super class in the constructor. (We will look at the meaning of bubbles shortly.)

4 Make reason accessible via a property.

The dog can now use this custom event when barking:

28

More magazines by this user
Similar magazines