Inherits from NSObject
Declared in SPEventDispatcher.h

Overview

The SPEventDispatcher class is the base for all classes that dispatch events.

The event mechanism is a key feature of Sparrow’s architecture. Objects can communicate with each other through events.

An event dispatcher can dispatch events (objects of type SPEvent or one of its subclasses) to objects that have registered themselves as listeners. A string (the event type) is used to identify different events.

Here is a sample:

// dispatching an event
[self dispatchEvent:[SPEvent eventWithType:@"eventType"]];

// listening to an event from 'object'
[object addEventListener:@selector(onEvent:) atObject:self forType:@"eventType"];

// the corresponding event listener
- (void)onEvent:(SPEvent *)event
{
    // an event was triggered
}

Alternatively, you can use blocks as event listeners:

[object addEventListenerForType:@"eventType" block:^(SPEvent *event)
 {
     // the event was triggered
 }];

Since SPDisplayObject (the base class of all rendered objects) inherits from SPEventDispatcher, the event mechanism is tightly bound to the display list. Events that have their bubbles-property enabled will rise up the display list until they reach its root (normally the stage). That means that a listener can register for the event type not only on the object that will dispatch it, but on any object that is a direct or indirect parent of the dispatcher.

Different to Adobe Flash, events in Sparrow do not have a capture-phase.

Tasks

Methods

Instance Methods

addEventListener:atObject:forType:

Registers an event listener at a certain object.

- (void)addEventListener:(SEL)selector atObject:(id)object forType:(NSString *)eventType

Discussion

Registers an event listener at a certain object.

Declared In

SPEventDispatcher.h

addEventListenerForType:block:

Registers an event listener which is implemented through a block.

- (void)addEventListenerForType:(NSString *)eventType block:(SPEventBlock)block

Discussion

Registers an event listener which is implemented through a block.

Declared In

SPEventDispatcher.h

dispatchEvent:

Dispatches an event to all objects that have registered for events of the same type.

- (void)dispatchEvent:(SPEvent *)event

Discussion

Dispatches an event to all objects that have registered for events of the same type.

Declared In

SPEventDispatcher.h

dispatchEventWithType:

Creates a new (non-bubbling) event object and dispatches it.

- (void)dispatchEventWithType:(NSString *)type

Discussion

Creates a new (non-bubbling) event object and dispatches it.

Declared In

SPEventDispatcher.h

dispatchEventWithType:bubbles:

Creates a new event object and dispatches it.

- (void)dispatchEventWithType:(NSString *)type bubbles:(BOOL)bubbles

Discussion

Creates a new event object and dispatches it.

Declared In

SPEventDispatcher.h

hasEventListenerForType:

Returns if there are listeners registered for a certain event type.

- (BOOL)hasEventListenerForType:(NSString *)eventType

Discussion

Returns if there are listeners registered for a certain event type.

Declared In

SPEventDispatcher.h

removeEventListener:atObject:forType:

Removes an event listener from an object.

- (void)removeEventListener:(SEL)selector atObject:(id)object forType:(NSString *)eventType

Discussion

Removes an event listener from an object.

Declared In

SPEventDispatcher.h

removeEventListenerForType:block:

Removes an event listener that was implemented through a block. Be careful in MRC mode: you have to pass the same copy of the block to both ‘addEventListener…’ and ‘removeEventListener…’. If you pass a stack block to either or both, removing won’t work.

- (void)removeEventListenerForType:(NSString *)eventType block:(SPEventBlock)block

Discussion

Removes an event listener that was implemented through a block. Be careful in MRC mode: you have to pass the same copy of the block to both ‘addEventListener…’ and ‘removeEventListener…’. If you pass a stack block to either or both, removing won’t work.

Declared In

SPEventDispatcher.h

removeEventListenersAtObject:forType:

Removes all event listeners from an object that have a certain type.

- (void)removeEventListenersAtObject:(id)object forType:(NSString *)eventType

Discussion

Removes all event listeners from an object that have a certain type.

Declared In

SPEventDispatcher.h