
キャラクターが画面の端っこまで歩いてきたら、マップをスクロールさせるiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface MoveScene : SKScene
@property BOOL contentCreated;
@property BOOL waite;
@property CGVector velocity;
@end
@implementation MoveScene
const int kDefaultNumberOfWalkFrames = 4;
const float showCharacterFramesOverOneSecond = 1.0f/(float) kDefaultNumberOfWalkFrames;
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.anchorPoint = CGPointMake(0.5, 0.5);
SKSpriteNode *myWorld = [[SKSpriteNode alloc] initWithColor:[SKColor greenColor] size:CGSizeMake(960, 568)];
myWorld.name = @”world”;
myWorld.position = CGPointMake(-320, 0);
[self addChild:myWorld];
SKSpriteNode *ground = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(960, 10)];
ground.position = CGPointMake(0, –284);
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
[myWorld addChild:ground];
for (int i = 0 ; i<2; i++) {
SKSpriteNode *wall = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(60, 60)];
wall.position = CGPointMake(-450 + 900 * i, –264);
wall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:wall.size];
wall.physicsBody.dynamic = NO;
[myWorld addChild:wall];
}
for (int i=0; i<3; i++) {
SKLabelNode *l = [[SKLabelNode alloc] init];
l.text = [NSString stringWithFormat:@”A-%d”, i+1];
l.position = CGPointMake((i-1) * 320, 150);
l.fontColor = [UIColor whiteColor];
l.fontSize = 100;
[myWorld addChild:l];
}
SKNode *camera = [SKNode node];
camera.name = @”camera”;
camera.position = CGPointMake(-320, 0);
[myWorld addChild:camera];
SKSpriteNode *walker = [SKSpriteNode spriteNodeWithImageNamed:@”walker01″];
walker.name = @”walker”;
walker.position = CGPointMake(-320, 0);
[myWorld addChild:walker];
SKTexture *f1 = [SKTexture textureWithImageNamed:@”walker01.png”];
SKTexture *f2 = [SKTexture textureWithImageNamed:@”walker02.png”];
SKTexture *f3 = [SKTexture textureWithImageNamed:@”walker03.png”];
NSArray *digTextures = @[f1,f3,f2,f3];
SKAction *animationFramesAction = [SKAction animateWithTextures:digTextures timePerFrame:showCharacterFramesOverOneSecond resize:YES restore:NO];
[walker runAction:[SKAction repeatActionForever:animationFramesAction]];
walker.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:walker.size];
self.velocity = CGVectorMake(50, 0);
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.velocity = CGVectorMake(-self.velocity.dx, 0);
}
– (void)didSimulatePhysics
{
SKNode *camera = [self childNodeWithName:@”//camera”];
[self centerOnNode:camera];
SKSpriteNode *walker = (SKSpriteNode*)[self childNodeWithName:@”//walker”];
walker.physicsBody.velocity = self.velocity;
SKNode *world = [self childNodeWithName:@”world”];
CGPoint checkPoint = [world convertPoint:walker.position toNode:camera];
if (checkPoint.x > 160 && !self.waite) {
self.waite = YES;
SKAction *move = [SKAction moveByX:320 y:0 duration:1.0];
[camera runAction:move completion:^{
self.waite = NO;
}];
}
if (checkPoint.x < –160 && !self.waite) {
self.waite = YES;
SKAction *move = [SKAction moveByX:-320 y:0 duration:1.0];
[camera runAction:move completion:^{
self.waite = NO;
}];
}
}
– (void)centerOnNode:(SKNode*)node
{
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x – cameraPositionInScene.x, node.parent.position.y – cameraPositionInScene.y);
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[MoveScene alloc] initWithSize:self.view.bounds.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end