Skip to content

Instantly share code, notes, and snippets.

veeneck / font
Created April 2, 2014 21:02
Referencing a custom font with Sprite Kit
View font
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"DINCond-RegularAlternate"];
veeneck / music.m
Last active August 29, 2015 13:58
Playing music with Sprite Kit
View music.m
-(void) loadSound {
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"otto_basetrack" withExtension:@"mp3"];
backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
backgroundMusicPlayer.numberOfLoops = -1;
[backgroundMusicPlayer prepareToPlay];
[backgroundMusicPlayer play];
veeneck / video.m
Last active August 29, 2015 13:58
Video playback with SpriteKit
View video.m
- (void) loadVideo {
SKVideoNode* introVideo = [SKVideoNode videoNodeWithVideoFileNamed:@""];
introVideo.position = CGPointMake(CGRectGetMidX(self.frame),
[self addChild: introVideo];
[introVideo play];
veeneck / selector.m
Created April 3, 2014 12:28
Sprite Kit performSelector with object
View selector.m
// Calling a function that has no parameters
[self performSelector:@selector(doImageFade) withObject:nil afterDelay:0.1];
// Calling a function that has one parameter.
// Notice the ':' after doImageFade and the use of withObject
[self performSelector:@selector(doImageFade:) withObject:img afterDelay:0.1];
veeneck / burst.m
Created April 3, 2014 22:24
Particle effects in Sprite Kit
View burst.m
NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"Snow" ofType:@"sks"];
SKEmitterNode *burstEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
burstEmitter.position = CGPointMake(CGRectGetMidX(self.frame),
[self addChild:burstEmitter];
veeneck / scenes.m
Created April 3, 2014 22:25
Switch scenes in Sprite Kit
View scenes.m
// Replace NAMEOFSCENE in code
-(void) switchScene {
SKTransition *reveal = [SKTransition fadeWithDuration:3];
SKScene *scene = [NAMEOFSCENE sceneWithSize:self.view.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:scene transition:reveal];
veeneck / curve.m
Created April 5, 2014 18:14
Follow curve to point with Sprite Kit
View curve.m
// Imagine shooting an arrow to a point
CGMutablePathRef path = CGPathCreateMutable();
// This will move an invisible path marker to a starting point
// In my case, the node is a child node of a sprite, so it is the local coords and not the screen coords
CGPathMoveToPoint(path, NULL, 10, 0);
// The 3-8th parameters are x/y coords. The arrow iwll first hit x:100 and y:50.
// It will then raise up a bit as it keeps going and finally drop to the target at x:300 y:0
CGPathAddCurveToPoint(path, NULL,
veeneck / text.m
Created April 7, 2014 12:18
Adding text with font in Sprite Kit
View text.m
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"DINCond-RegularAlternate"];
myLabel.text = @"it's happening";
myLabel.fontColor = [SKColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1]; /*#454545*/
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
[self addChild:myLabel];
veeneck / getter.m
Created April 7, 2014 14:35
Sprite Kit Getters & Setters
View getter.m
// in header file
BOOL isDying;
// in subclass where object is referenced
// for example, enemy = (Character*)target
if(enemy->isDying) {
// VS a getter in header
veeneck / cfroms.m
Created April 8, 2014 18:01
Class from string
View cfroms.m
// Instead of a switch on each name, use string directly.
_thisClass = NSClassFromString(@"DynamicStringRepresentingClass");