
バレーボールをレシーブするロボットといった感じでiPhoneアプリのサンプルコードを描いてみます。
@import SpriteKit;
#import “ViewController.h”
@interface RoboScene : SKScene
@property (nonatomic) NSInteger move;
@property (nonatomic) BOOL handsup;
@end
@implementation RoboScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithRed:0.7 green:0.7 blue:1.0 alpha:1];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.speed = 0.5;
[self createReceiverRobo];
[self playGame];
}
– (void)createReceiverRobo
{
SKNode *robo = [SKNode node];
robo.name = @”robo”;
robo.position = CGPointMake(CGRectGetMaxX(self.frame) * 0.75, 80);
[self addChild:robo];
UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:30 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *head = [SKShapeNode node];
head.path = circle.CGPath;
head.fillColor = [SKColor brownColor];
head.lineWidth = 0;
head.position = CGPointMake(0, 50);
[robo addChild:head];
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(40, 50)];
[robo addChild:body];
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(10, 30)];
bar.position = CGPointMake(0, –40);
[robo addChild:bar];
UIBezierPath *circleTire = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *tire = [SKShapeNode node];
tire.name = @”tire”;
tire.path = circleTire.CGPath;
tire.position = CGPointMake(robo.position.x, robo.position.y–50);
tire.lineWidth = 10;
tire.strokeColor = [SKColor blackColor];
tire.fillColor = [SKColor lightGrayColor];
[self addChild:tire];
SKSpriteNode *hand = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(20, 50)];
hand.name = @”hand”;
hand.position = robo.position;
[self addChild:hand];
hand.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hand.size];
SKAction *flash = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction fadeOutWithDuration:0.5], [SKAction fadeInWithDuration:1.0]]]];
SKLabelNode *right = [SKLabelNode node];
right.name = @”right”;
right.text = @”<“;
right.fontSize = 60;
right.position = CGPointMake(-70, 0);
[robo addChild:right];
[right runAction:flash];
SKLabelNode *left = [SKLabelNode node];
left.name = @”left”;
left.text = @”>”;
left.fontSize = 60;
left.position = CGPointMake(70, 0);
[robo addChild:left];
[left runAction:flash];
robo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 80)];
robo.physicsBody.allowsRotation = NO;
robo.physicsBody.friction = 0;
tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];
tire.physicsBody.friction = 0;
SKPhysicsJointPin *pinT = [SKPhysicsJointPin jointWithBodyA:robo.physicsBody bodyB:tire.physicsBody anchor:tire.position];
[self.physicsWorld addJoint:pinT];
SKPhysicsJointPin *pinH = [SKPhysicsJointPin jointWithBodyA:robo.physicsBody bodyB:hand.physicsBody anchor:CGPointMake(hand.position.x, hand.position.y + 30)];
pinH.shouldEnableLimits = YES;
pinH.lowerAngleLimit = –M_PI * 0.5;
pinH.upperAngleLimit = M_PI * 0.1;
[self.physicsWorld addJoint:pinH];
}
– (void)playGame
{
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(createBall) userInfo:nil repeats:YES];
}
– (void)createBall
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:YES];
SKShapeNode *ball = [SKShapeNode node];
ball.path = path.CGPath;
ball.fillColor = [SKColor whiteColor];
ball.position = CGPointMake(50, 160);
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];
ball.physicsBody.restitution = 0.8;
ball.physicsBody.density = 0.1;
[ball.physicsBody applyImpulse:CGVectorMake(1, 1)];
[ball runAction:[SKAction sequence:@[[SKAction waitForDuration:5.0], [SKAction runBlock:^{
[ball removeFromParent];
}]]]];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *n = [self nodeAtPoint:p];
if ([n isKindOfClass:[SKLabelNode class]]) {
if ([((SKLabelNode *)n).text isEqual:@”<“]) {
self.move = –1;
} else if ([((SKLabelNode *)n).text isEqual:@”>”]) {
self.move = 1;
}
} else if ([n.name isEqual:@”tire”]) {
self.handsup = YES;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.move = 0;
self.handsup = NO;
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *robo = [self childNodeWithName:@”robo”];
robo.physicsBody.velocity = CGVectorMake(self.move * 200, 0);
SKNode *hand = [self childNodeWithName:@”hand”];
if (self.handsup) {
[hand.physicsBody applyForce:CGVectorMake(-100, 0)];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[RoboScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end