Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@veeneck
veeneck / font
Created April 2, 2014 21:02
Referencing a custom font with Sprite Kit
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"DINCond-RegularAlternate"];
@veeneck
veeneck / music.m
Last active August 29, 2015 13:58
Playing music with Sprite Kit
-(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
veeneck / video.m
Last active August 29, 2015 13:58
Video playback with SpriteKit
- (void) loadVideo {
SKVideoNode* introVideo = [SKVideoNode videoNodeWithVideoFileNamed:@"gameandcode_mockup_forryan.mov"];
introVideo.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild: introVideo];
[introVideo play];
}
@veeneck
veeneck / selector.m
Created April 3, 2014 12:28
Sprite Kit performSelector with object
// 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
veeneck / burst.m
Created April 3, 2014 22:24
Particle effects in Sprite Kit
NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"Snow" ofType:@"sks"];
SKEmitterNode *burstEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
burstEmitter.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[self addChild:burstEmitter];
@veeneck
veeneck / scenes.m
Created April 3, 2014 22:25
Switch scenes in Sprite Kit
// 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
veeneck / curve.m
Created April 5, 2014 18:14
Follow curve to point with Sprite Kit
// 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
veeneck / text.m
Created April 7, 2014 12:18
Adding text with font in Sprite Kit
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),
CGRectGetMidY(self.frame));
[self addChild:myLabel];
@veeneck
veeneck / getter.m
Created April 7, 2014 14:35
Sprite Kit Getters & Setters
// 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
veeneck / cfroms.m
Created April 8, 2014 18:01
Class from string
// Instead of a switch on each name, use string directly.
_thisClass = NSClassFromString(@"DynamicStringRepresentingClass");