Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple category for Sparrow that allows one to change the loopability of a SPDisplayObject.
//
// SPDisplayObject+Loopability.h
// Sparrow
//
// Created by Shilo White on 11/9/11.
// Copyright (c) 2011 Shilocity Productions. All rights reserved.
//
#import "SPDisplayObject.h"
@interface SPDisplayObject (Loopability)
@property (nonatomic, assign, readwrite) BOOL loopable;
@end
//
// SPDisplayObject+Loopability.m
// Sparrow
//
// Created by Shilo White on 11/9/11.
// Copyright (c) 2011 Shilocity Productions. All rights reserved.
//
#import "SPDisplayObject+Loopability.h"
#import "SPEventDispatcher.h"
#import "SPEnterFrameEvent.h"
#import "SPEvent_Internal.h"
#import <objc/runtime.h>
static char sLoopable;
@implementation SPDisplayObject (Loopability)
@dynamic loopable;
- (void)setLoopable:(BOOL)loopable {
if ([self loopable] != loopable) {
objc_setAssociatedObject(self, &sLoopable, [NSNumber numberWithBool:loopable], OBJC_ASSOCIATION_ASSIGN);
}
}
- (BOOL)loopable {
NSNumber *loopable = objc_getAssociatedObject(self, &sLoopable);
return (loopable)?[loopable boolValue]:YES;
}
@end
@implementation SPEventDispatcher (Loopability)
- (void)dispatchEvent:(SPEvent*)event {
NSMutableArray *listeners = [mEventListeners objectForKey:event.type];
if (!event.bubbles && !listeners) return;
SPEventDispatcher *previousTarget = event.target;
if (!previousTarget || event.currentTarget) event.target = self;
if (event.type == SP_EVENT_TYPE_ENTER_FRAME) {
SPDisplayObject *currentObject = (SPDisplayObject *)event.currentTarget;
if (!currentObject) currentObject = (SPDisplayObject *)event.target;
if (!currentObject.loopable) return;
while (currentObject.parent) {
currentObject = (SPDisplayObject *)currentObject.parent;
if (!currentObject.loopable) return;
}
}
[self retain];
BOOL stopImmediatePropagation = NO;
if (listeners.count != 0)
{
event.currentTarget = self;
[listeners retain];
for (NSInvocation *inv in listeners)
{
[inv setArgument:&event atIndex:2];
[inv invoke];
if (event.stopsImmediatePropagation)
{
stopImmediatePropagation = YES;
break;
}
}
[listeners release];
}
if (!stopImmediatePropagation && event.bubbles && !event.stopsPropagation &&
[self isKindOfClass:[SPDisplayObject class]])
{
event.currentTarget = nil;
SPDisplayObject *target = (SPDisplayObject*)self;
[target.parent dispatchEvent:event];
}
if (previousTarget) event.target = previousTarget;
[self autorelease];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.