iPhoneさかなあぷり

お魚を左のバケツに入れると赤い色、右のバケツに入れると青い色に変身するようなiPhoneアプリのサンプルコードを描いてみます。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface ColorChangeScene : SKScene

@property BOOL contentCreated;

@end

@implementation ColorChangeScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = ColorHex(0xF1C0A2);

    

    SKSpriteNode *left = [SKSpriteNode spriteNodeWithImageNamed:@”arrowleft”];

    left.position = CGPointMake(150, 250);

    left.name = @”left”;

    [self addChild:left];

    

    SKSpriteNode *right = [SKSpriteNode spriteNodeWithImageNamed:@”arrowright”];

    right.position = CGPointMake(CGRectGetMaxX(self.frame) – 150, 250);

    right.name = @”right”;

    [self addChild:right];

    

    SKSpriteNode *fish = [SKSpriteNode spriteNodeWithImageNamed:@”fish”];

    fish.position = CGPointMake(CGRectGetMidX(self.frame), 230);

    fish.name = @”fish”;

    fish.zPosition = 1;

    [self addChild:fish];

    

    SKSpriteNode *hotbath = [SKSpriteNode spriteNodeWithImageNamed:@”hot”];

    hotbath.position = CGPointMake(150, 80);

    hotbath.name = @”hot”;

    [self addChild:hotbath];

    

    SKSpriteNode *cold = [SKSpriteNode spriteNodeWithImageNamed:@”cold”];

    cold.position = CGPointMake(CGRectGetMaxX(self.frame)-150, 80);

    cold.name = @”cold”;

    [self addChild:cold];

    

    SKAction *fadeOut = [SKAction fadeAlphaTo:0.3 duration:1.0];

    SKAction *fadeIn = [SKAction fadeInWithDuration:1.0];

    SKAction *flashTypeA = [SKAction repeatActionForever:[SKAction sequence:@[fadeOut, fadeIn]]];

    SKAction *flashTypeB = [SKAction repeatActionForever:[SKAction sequence:@[fadeIn, fadeOut]]];

    

    [right runAction:flashTypeA];

    [left runAction:flashTypeB];

    

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    

    SKNode *fish = [self childNodeWithName:@”fish”];

    

    if ([[self childNodeWithName:@”left”] containsPoint:p]) {

        SKAction *jump = [SKAction moveTo:CGPointMake(150, 300) duration:0.2];

        SKAction *moveToHot = [SKAction moveTo:CGPointMake(150, 100) duration:0.5];

        

        SKAction *colorChange = [SKAction colorizeWithColor:ColorHex(0xD72B5D) colorBlendFactor:1.0 duration:3.0];

        [fish runAction:[SKAction sequence:@[jump, moveToHot, colorChange]]];

    } else {

        SKAction *jump = [SKAction moveTo:CGPointMake(CGRectGetMaxX(self.frame)-150, 300) duration:0.2];

        SKAction *moveToHot = [SKAction moveTo:CGPointMake(CGRectGetMaxX(self.frame)-150, 100) duration:0.5];

        

        SKAction *colorChange = [SKAction colorizeWithColor:ColorHex(0x51B5C5) colorBlendFactor:1.0 duration:3.0];

        [fish runAction:[SKAction sequence:@[jump, moveToHot, colorChange]]];

    }

    

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 568, 320)];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[ColorChangeScene alloc] initWithSize:spriteView.bounds.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end