Skip to content

Commit

Permalink
updates scenes: 1,2,3
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnboy committed Feb 26, 2014
1 parent be8d5e5 commit 5f8580e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 90 deletions.
54 changes: 33 additions & 21 deletions SKDemo/SKDemo/MyScene1.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,54 @@
#import "MyScene1.h"

@interface MyScene1 ()
@property (nonatomic) SKSpriteNode *ship;
@end

static inline CGPoint ADD(const CGPoint a, const CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
@property (nonatomic) SKSpriteNode *alien;

@end

@implementation MyScene1

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
NSLog(@"MyScene1: sz=%.1fx%.1f", size.width, size.height);

self.backgroundColor = [SKColor colorWithRed:0.1 green:0.2 blue:0.3 alpha:1.0];
self.backgroundColor = [SKColor colorWithRed:0.1f green:0.1f blue:0.3f alpha:1.0f];

_ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
_ship.position = CGPointMake(0, 0);
_ship.xScale = 0.25;
_ship.yScale = 0.25;
[self addChild:_ship];
_alien = [SKSpriteNode spriteNodeWithImageNamed:@"alien1.png"];
_alien.position = CGPointMake(0.0f, 0.0f);
[self addChild:_alien];
}
return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
_ship.position = loc;
}
#pragma mark - game loop

-(void)update:(CFTimeInterval)currentTime {
// 1. Make it move faster, slower, backwards, etc...
// 2. Make it get bigger, smaller (xScale, yScale)
// 3. Make it rotate (zRotation)
//move it
_alien.position = CGPointMake(_alien.position.x + 1, _alien.position.y + 1);

// 1. Make it move faster
//_alien.position = CGPointMake(_alien.position.x + 2, _alien.position.y + 2);

// 2. Make it get bigger, smaller
//_alien.xScale = ?
//_alien.yScale = ?

// 3. Make it rotate
//_alien.zRotation = ?

// 4. Make it bounce at the edges...
_ship.position = ADD(_ship.position, CGPointMake(1,1));
}

@end
#pragma mark - touch handler

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint pos = [touch locationInNode:self];
NSLog(@"TOUCH: (%.1f,%.1f)", pos.x, pos.y);

// move alien to touch pos
_alien.position = pos;
}

@end
59 changes: 38 additions & 21 deletions SKDemo/SKDemo/MyScene2.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,53 @@ -(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
NSLog(@"MyScene2: sz=%.1fx%.1f", size.width, size.height);

self.backgroundColor = [SKColor colorWithRed:0.2 green:0.3 blue:0.1 alpha:1.0];
self.backgroundColor = [SKColor colorWithRed:0.1f green:0.3f blue:0.1f alpha:1.0f];
}
return self;
}

#pragma mark - game loop

-(void)update:(CFTimeInterval)currentTime {
//do nothing...
}

#pragma mark - touch handler

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
CGPoint pos = [touch locationInNode:self];
NSLog(@"TOUCH: (%.1f,%.1f)", pos.x, pos.y);

SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
ship.position = loc;
ship.xScale = 0.2;
ship.yScale = 0.2;
[self addChild:ship];
SKSpriteNode *alien = [SKSpriteNode spriteNodeWithImageNamed:@"alien1.png"];
alien.position = pos;
[self addChild:alien];

SKAction *hit = [SKAction playSoundFileNamed:@"fire.wav" waitForCompletion:NO];
SKAction *miss = [SKAction playSoundFileNamed:@"miss.wav" waitForCompletion:NO];

SKAction *move = [SKAction moveByX:-25.0 y:-25.0 duration:1.0];
SKAction *rotate = [SKAction rotateByAngle:-M_PI_2 duration:1];

SKAction *fade = [SKAction fadeOutWithDuration:1.5];
SKAction *move = [SKAction moveByX:13.0f y:37.0f duration:0.5f];
SKAction *rotate = [SKAction rotateByAngle:-M_PI duration:0.5f];
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.5f];
SKAction *remove = [SKAction removeFromParent];

// 1. Other actions to try: colorize, fadeIn, repeat, scaleBy, wait
[ship runAction:[SKAction sequence:@[
arc4random_uniform(7) < 5 ? hit : miss,
[SKAction group:@[move, rotate]],
fade,
remove]]];
[alien runAction:[SKAction sequence:@[move, rotate, fadeOut, remove]]];

// Try some other actions...

// 1. wait
//SKAction *wait = [SKaction waitForDuration:1.0f];

// 2. fadeIn
//SKAction *fadeIn = [SKAction fadeInWithDuration:1.0f];

// 3. scale
//SKAction *doubleSize = [SKAction scaleTo:2.0f duration:1.0f];
//SKAction *halfSize = [SKAction scaleTo:0.5f duration:1.0f];

// 4. colorize
//SKAction *blue = [SKAction colorizeWithColor:[UIColor blueColor] colorBlendFactor:1.0f duration:1.0f];
//SKAction *green = [SKAction colorizeWithColor:[UIColor greenColor] colorBlendFactor:1.0f duration:1.0f];

// 5. actions in parallel
//SKAction *parallel = [SKAction group:@[move,rotate]];
}

@end
@end
78 changes: 30 additions & 48 deletions SKDemo/SKDemo/MyScene3.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,69 @@
#import "MyScene3.h"

@interface MyScene3 ()

@property (nonatomic) SKSpriteNode *alien;
@property (nonatomic) CGPoint center;
@property (nonatomic) SKAction *flyAnim;
@property (nonatomic) SKAction *hit;
@property (nonatomic) SKAction *miss;
@end

static inline CGPoint ADD(const CGPoint a, const CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
@end

@implementation MyScene3

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
NSLog(@"MyScene3: sz=%.1fx%.1f", size.width, size.height);

self.backgroundColor = [SKColor colorWithRed:0.3 green:0.1 blue:0.1 alpha:1.0];
self.backgroundColor = [SKColor colorWithRed:0.3f green:0.1f blue:0.1f alpha:1.0f];

//init center
_center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_center = CGPointMake(size.width * 0.5f, size.height * 0.5f);

//init sounds
_hit = [SKAction playSoundFileNamed:@"fire.wav" waitForCompletion:NO];
_miss = [SKAction playSoundFileNamed:@"miss.wav" waitForCompletion:NO];

//create atlas
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"alien"];
SKTexture *a1 = [atlas textureNamed:@"alien1.png"];
SKTexture *a2 = [atlas textureNamed:@"alien2.png"];
SKTexture *a3 = [atlas textureNamed:@"alien3.png"];
SKTexture *a4 = [atlas textureNamed:@"alien4.png"];
SKTexture *a5 = [atlas textureNamed:@"alien5.png"];
SKTexture *a6 = [atlas textureNamed:@"alien6.png"];
SKTexture *a7 = [atlas textureNamed:@"alien7.png"];
SKTexture *a8 = [atlas textureNamed:@"alien8.png"];
SKTexture *a9 = [atlas textureNamed:@"alien9.png"];
SKTexture *a10 = [atlas textureNamed:@"alien10.png"];
NSArray *alienFly = @[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a9,a8,a7,a6,a5,a4,a3,a2,a1];

//create sprite
_alien = [SKSpriteNode spriteNodeWithTexture:a1];
//init sprite
_alien = [SKSpriteNode spriteNodeWithImageNamed:@"alien1.png"];
_alien.position = _center;
[self addChild:_alien];

//1. Try faster, slower, repeating texture anim
_flyAnim = [SKAction animateWithTextures:alienFly timePerFrame:0.1];
}
return self;
}


#pragma mark - game loop

-(void)update:(CFTimeInterval)currentTime {
//do nothing...
}

#pragma mark - touch handler

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
CGPoint pos = [touch locationInNode:self];
NSLog(@"TOUCH: (%.1f,%.1f)", pos.x, pos.y);

if ([_alien containsPoint:loc]) {
//2. Try to adjust positioning for iPhone vs iPad
float x = (float)arc4random_uniform(201) - 100.0f;
float y = (float)arc4random_uniform(201) - 100.0f;
NSLog(@"HIT!: (%.1f,%.1f)", x, y);
if ([_alien containsPoint:pos]) {
NSLog(@"HIT!");

SKAction *fadeOut = [SKAction fadeOutWithDuration:0.1];
SKAction *fadeIn = [SKAction fadeInWithDuration:0.1];
SKAction *move = [SKAction moveTo:ADD(_center, CGPointMake(x,y)) duration:0.1];
SKAction *bg = [SKAction runBlock:^{
float red = CGColorGetComponents(self.backgroundColor.CGColor)[0] + 0.04f;
if (red > 0.8) { red = 0.3f; }
self.backgroundColor = [SKColor colorWithRed:red green:0.1 blue:0.1 alpha:1.0];
}];
//2. How would you adjust positioning for iPhone vs iPad

//compute the new position
CGPoint newPos = CGPointMake(_center.x + arc4random_uniform(201) - 100.0f,
_center.y + arc4random_uniform(201) - 100.0f);

SKAction *fadeOut = [SKAction fadeOutWithDuration:0.2f];
SKAction *fadeIn = [SKAction fadeInWithDuration:0.2f];
SKAction *move = [SKAction moveTo:newPos duration:0.2f];

[_alien runAction:[SKAction sequence:@[
_hit,
fadeOut,
[SKAction group:@[move,bg]],
fadeIn,
_flyAnim]]
withKey:@"ANIM"];
[_alien runAction:[SKAction sequence:@[_hit,fadeOut,move,fadeIn]] withKey:@"ANIM"];
} else {
NSLog(@"miss");
[_alien runAction:_miss];
}
}

@end
@end

0 comments on commit 5f8580e

Please sign in to comment.