Skip to content
This repository has been archived by the owner on Jul 19, 2018. It is now read-only.

Commit

Permalink
SpriteKit demo: a cloud, hats and fire!
Browse files Browse the repository at this point in the history
  • Loading branch information
bolot committed Sep 20, 2013
1 parent aeb095d commit ce88520
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 14 deletions.
20 changes: 20 additions & 0 deletions SpritesInMotion/SpritesInMotion.xcodeproj/project.pbxproj
Expand Up @@ -23,6 +23,11 @@
DA49760217E88CFA0048115D /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA4975DD17E88CF90048115D /* UIKit.framework */; };
DA49760A17E88CFA0048115D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = DA49760817E88CFA0048115D /* InfoPlist.strings */; };
DA49760C17E88CFA0048115D /* SpritesInMotionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DA49760B17E88CFA0048115D /* SpritesInMotionTests.m */; };
DA49761617E9F7780048115D /* cloud@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DA49761517E9F7780048115D /* cloud@2x.png */; };
DA49761817E9FAAD0048115D /* bnrhat@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DA49761717E9FAAD0048115D /* bnrhat@2x.png */; };
DA49761F17EA12F50048115D /* fire.sks in Resources */ = {isa = PBXBuildFile; fileRef = DA49761D17EA12F50048115D /* fire.sks */; };
DA49762017EA12F50048115D /* spark.png in Resources */ = {isa = PBXBuildFile; fileRef = DA49761E17EA12F50048115D /* spark.png */; };
DA49762417EA232A0048115D /* roof@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DA49762317EA232A0048115D /* roof@2x.png */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -58,6 +63,11 @@
DA49760717E88CFA0048115D /* SpritesInMotionTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SpritesInMotionTests-Info.plist"; sourceTree = "<group>"; };
DA49760917E88CFA0048115D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
DA49760B17E88CFA0048115D /* SpritesInMotionTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SpritesInMotionTests.m; sourceTree = "<group>"; };
DA49761517E9F7780048115D /* cloud@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "cloud@2x.png"; sourceTree = "<group>"; };
DA49761717E9FAAD0048115D /* bnrhat@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "bnrhat@2x.png"; sourceTree = "<group>"; };
DA49761D17EA12F50048115D /* fire.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = fire.sks; sourceTree = "<group>"; };
DA49761E17EA12F50048115D /* spark.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = spark.png; sourceTree = "<group>"; };
DA49762317EA232A0048115D /* roof@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "roof@2x.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -126,7 +136,12 @@
DA4975F117E88CF90048115D /* BNRViewController.m */,
DA4975F317E88CF90048115D /* BNRMyScene.h */,
DA4975F417E88CF90048115D /* BNRMyScene.m */,
DA49761517E9F7780048115D /* cloud@2x.png */,
DA49761717E9FAAD0048115D /* bnrhat@2x.png */,
DA49762317EA232A0048115D /* roof@2x.png */,
DA4975F817E88CF90048115D /* Images.xcassets */,
DA49761D17EA12F50048115D /* fire.sks */,
DA49761E17EA12F50048115D /* spark.png */,
DA4975E217E88CF90048115D /* Supporting Files */,
);
path = SpritesInMotion;
Expand Down Expand Up @@ -239,8 +254,13 @@
buildActionMask = 2147483647;
files = (
DA4975E617E88CF90048115D /* InfoPlist.strings in Resources */,
DA49762417EA232A0048115D /* roof@2x.png in Resources */,
DA49761817E9FAAD0048115D /* bnrhat@2x.png in Resources */,
DA4975F917E88CF90048115D /* Images.xcassets in Resources */,
DA49762017EA12F50048115D /* spark.png in Resources */,
DA4975EF17E88CF90048115D /* Main.storyboard in Resources */,
DA49761617E9F7780048115D /* cloud@2x.png in Resources */,
DA49761F17EA12F50048115D /* fire.sks in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
51 changes: 40 additions & 11 deletions SpritesInMotion/SpritesInMotion/BNRMyScene.m
Expand Up @@ -19,14 +19,20 @@ + (instancetype)randomColor
}
@end

@interface BNRMyScene ()
@property (nonatomic, assign) NSUInteger numberOfHats;
@end

@implementation BNRMyScene

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.9 green:0.95 blue:0.8 alpha:1.0];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.5 alpha:1.0];
[self configurePhysicsWorld];
[self createCloud];
[self createBuilding];
self.numberOfHats = 0;
}
return self;
}
Expand All @@ -36,7 +42,10 @@ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
[self createBouncingBallAtPoint:location];
[self createBouncingHatAtPoint:location];
if (++self.numberOfHats == 3) {
[self createEmitter];
}
}
}

Expand All @@ -46,16 +55,13 @@ - (void)configurePhysicsWorld
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 0, self.size.width, self.size.height)];
}

- (void)createBouncingBallAtPoint:(CGPoint)point
- (void)createBouncingHatAtPoint:(CGPoint)point
{
CGFloat radius = 15.0;
// for physics simulation, we'll pretend it's a circle
CGFloat radius = 20.0;
SKPhysicsBody *physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
physicsBody.velocity = CGVectorMake(300+arc4random_uniform(300), 200+arc4random_uniform(300));
physicsBody.restitution = 0.8; // make it bouncy
CGSize size = CGSizeMake(radius*2, radius*2);
// ...but will render it as a square to see the rotation
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithColor:[SKColor randomColor] size:size];
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"bnrhat"];
ball.position = point;
ball.physicsBody = physicsBody;
[self addChild:ball];
Expand All @@ -68,22 +74,45 @@ - (void)createBuilding
one.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
one.position = CGPointMake(self.size.width-60, 180);
one.physicsBody.restitution = 0.5;
SKSpriteNode *two = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:size];
SKSpriteNode *two = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:size];
two.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
two.position = CGPointMake(self.size.width-160, 180);
two.physicsBody.restitution = 0.5;
[self addChild:one];
[self addChild:two];
CGSize horiz = CGSizeMake(150, 25);
SKSpriteNode *three = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:horiz];
SKSpriteNode *three = [SKSpriteNode spriteNodeWithImageNamed:@"roof"];
three.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:horiz];
three.position = CGPointMake(self.size.width-110, 300);
three.physicsBody.restitution = 0.5;
[self addChild:three];
}

- (void)createCloud
{
SKSpriteNode *cloud = [SKSpriteNode spriteNodeWithImageNamed:@"cloud"];
cloud.position = CGPointMake(60, 280);
SKAction *goRight = [SKAction moveByX:300 y:0 duration:2];
SKAction *goLeft = [goRight reversedAction];
SKAction *action = [SKAction sequence:@[goRight, goLeft]];
[cloud runAction:[SKAction repeatActionForever:action]];
[self addChild:cloud];
}

- (void)createEmitter
{
[self addChild:[self newFireEmitter]];
}

- (SKEmitterNode *) newFireEmitter
{
NSString *firePath = [[NSBundle mainBundle] pathForResource:@"fire" ofType:@"sks"];
SKEmitterNode *fire = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath];
fire.position = CGPointMake(self.size.width-110, 10);
return fire;
}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end
6 changes: 3 additions & 3 deletions SpritesInMotion/SpritesInMotion/BNRViewController.m
Expand Up @@ -20,9 +20,9 @@ - (void)viewDidLoad
- (void)viewDidAppear:(BOOL)animated
{
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// SKView * skView = (SKView *)self.view;
// skView.showsFPS = YES;
// skView.showsNodeCount = YES;

[self resetScene];
}
Expand Down
Binary file added SpritesInMotion/SpritesInMotion/bnrhat@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SpritesInMotion/SpritesInMotion/cloud@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SpritesInMotion/SpritesInMotion/fire.sks
Binary file not shown.
Binary file added SpritesInMotion/SpritesInMotion/roof@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SpritesInMotion/SpritesInMotion/spark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ce88520

Please sign in to comment.