Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created November 10, 2011 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/1353975 to your computer and use it in GitHub Desktop.
Save Shilo/1353975 to your computer and use it in GitHub Desktop.
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