
キャラをモソモソと歩かせるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface WalkScene : SKScene
@property (nonatomic) BOOL walking;
@end
@implementation WalkScene
– (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[self createBackgroundStripe];
[self createWalker];
[self createButton];
}
– (void)createWalker
{
UIBezierPath *pathA = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-30, –30, 60, 60) cornerRadius:15];
SKShapeNode *body = [SKShapeNode node];
body.name = @”walker”;
body.path = pathA.CGPath;
body.position = CGPointMake(50, 60);
[self addChild:body];
body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(60, 90)];
UIBezierPath *pathB = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-5, –8, 10, 16) cornerRadius:5];
SKShapeNode *footR = [SKShapeNode node];
footR.name = @”footR”;
footR.path = pathB.CGPath;
footR.position = CGPointMake(20, –38);
[body addChild:footR];
SKShapeNode *footL = [SKShapeNode node];
footL.name = @”footL”;
footL.path = pathB.CGPath;
footL.position = CGPointMake(-20, –38);
[body addChild:footL];
for (int i=0; i<2; i++) {
SKSpriteNode *eye = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(5, 5)];
eye.position = CGPointMake(10 + 10*i, 10);
[body addChild:eye];
}
}
– (void)createButton
{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-50, –20, 100, 40) cornerRadius:20];
SKShapeNode *btn = [SKShapeNode node];
btn.name = @”button”;
btn.path = path.CGPath;
btn.fillColor = [SKColor whiteColor];
btn.position = CGPointMake(CGRectGetMaxX(self.frame)-30, 100);
[self addChild:btn];
SKLabelNode *l = [SKLabelNode node];
l.text = @”walk”;
l.fontColor = [SKColor blackColor];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
[btn addChild:l];
}
– (void)createBackgroundStripe
{
for (int i=0; i<10; i++) {
SKSpriteNode *s = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(30, 400)];
s.zRotation = M_PI / 5.0;
s.position = CGPointMake(i * 80, CGRectGetMidY(self.frame));
[self addChild:s];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *btn = [self childNodeWithName:@”button”];
if ([btn containsPoint:p]) {
SKNode *walker = [self childNodeWithName:@”walker”];
SKNode *footR = [walker childNodeWithName:@”footR”];
SKNode *footL = [walker childNodeWithName:@”footL”];
SKAction *front = [SKAction moveByX:5 y:0 duration:0.2];
SKAction *back = [SKAction moveByX:-5 y:0 duration:0.2];
SKAction *wait = [SKAction waitForDuration:0.2];
[footR runAction:[SKAction repeatActionForever:[SKAction sequence:@[back, wait, front, wait]]]];
[footL runAction:[SKAction repeatActionForever:[SKAction sequence:@[front, wait, back, wait]]]];
self.walking = YES;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.walking = NO;
SKNode *walker = [self childNodeWithName:@”walker”];
[walker.children makeObjectsPerformSelector:@selector(removeAllActions)];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *walker = [self childNodeWithName:@”walker”];
if (self.walking) {
walker.physicsBody.velocity = CGVectorMake(20, 0);
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[WalkScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end