Skip to content

Instantly share code, notes, and snippets.

@naterhat
Created November 1, 2014 19:23
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 naterhat/5399eec40eaa23edbfbc to your computer and use it in GitHub Desktop.
Save naterhat/5399eec40eaa23edbfbc to your computer and use it in GitHub Desktop.
Test allocating and deallocating the scene.
// Created by Nate
//
// This is created for the purpose of testing of allocating and deallocating a scene that use a weak and a strong reference.
// - TEST_WEAK is used to toggle weak and strong
// - Line 68, the block action is in the middle of the stack. The action sequence can be switch around to test the allocating and deallocating the scene.
#import "TestScene.h"
/*
This is used to test a weak reference or a strong reference
*/
#define TEST_WEAK 1
static NSUInteger _count = 0;
@implementation TestScene
{
SKLabelNode *_label;
}
- (void)dealloc
{
NSLog(@"Test Scene %@ DEALLOCATED", self.name);
}
- (instancetype)initWithSize:(CGSize)size
{
if(self = [super initWithSize:size]){
// increment count
_count++;
// setup scene
self.name = @(_count).stringValue;
[self setAnchorPoint:CGPointMake(.5, .5)];
// setup label
SKLabelNode *label = [[SKLabelNode alloc] init];
[label setText:@"TEST TEST"];
[self addChild:label];
_label = label;
// log
NSLog(@"Test Scene %@ is Added", self.name);
} return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([_label hasActions]) return;
__weak typeof(self) weakself = self;
// setup actions
SKAction *rotate = [SKAction rotateByAngle:360 * M_PI/180 duration:2.0f];
SKAction *wait = [SKAction waitForDuration:1.0f];
SKAction *blockAction = [SKAction runBlock:^{
#if TEST_WEAK
[weakself moveNextScene];
#else
[self moveNextScene];
#endif
}];
/*
the block action is the middle of the sequence.
*/
SKAction *seq = [SKAction sequence:@[rotate,
blockAction,
wait]];
// run actions
[_label runAction:seq];
}
- (void)moveNextScene
{
// present next scene with transition
SKTransition *transition = [SKTransition fadeWithDuration:1.0f];
SKScene *newScene = [[[self class] alloc] initWithSize:self.size];
[self.view presentScene:newScene transition:transition];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment