
おもちゃのフリースローっぽいiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ThrowScene : SKScene
@end
@implementation ThrowScene
– (void)didMoveToView:(SKView *)view
{
self.view.backgroundColor = [SKColor brownColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, 0) toPoint:CGPointMake(100, 0)];
[self createPlayer];
[self createBall];
[self createGoal];
[self createButton];
}
– (void)createPlayer
{
SKNode *player = [SKNode node];
player.name = @”player”;
player.position = CGPointMake(50, 50);
[self addChild:player];
SKSpriteNode *head = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
head.position = CGPointMake(0, 30);
[player addChild:head];
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(5, 50)];
body.position = CGPointMake(0, 10);
[player addChild:body];
player.physicsBody = [SKPhysicsBody bodyWithBodies:@[[SKPhysicsBody bodyWithRectangleOfSize:head.size center:head.position], [SKPhysicsBody bodyWithRectangleOfSize:body.size center:body.position]]];
player.physicsBody.allowsRotation = NO;
SKSpriteNode *arm = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(30, 5)];
arm.position = CGPointMake(15, 18);
arm.zRotation = 0.4;
[player addChild:arm];
}
– (void)createBall
{
SKNode *player = [self childNodeWithName:@”player”];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *ball = [SKShapeNode node];
ball.name = @”ball”;
ball.path = path.CGPath;
ball.fillColor = [SKColor brownColor];
ball.position = CGPointMake(30, 30);
[player addChild:ball];
}
– (void)createGoal
{
SKSpriteNode *board = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(10, 100)];
board.position = CGPointMake(CGRectGetMaxX(self.frame) – 30, CGRectGetMaxY(self.frame) – 70);
[self addChild:board];
board.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:board.size];
board.physicsBody.dynamic = NO;
for (int i=0; i<2; i++) {
SKSpriteNode *net = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(2, 30)];
net.position = CGPointMake(board.position.x + (i ? –20 : –80), board.position.y–60);
net.zRotation = i ? –0.2 : 0.2;
[self addChild:net];
net.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:net.size];
net.physicsBody.dynamic = NO;
}
}
– (void)createButton
{
SKLabelNode *jump = [SKLabelNode node];
jump.name = @”jump”;
jump.text = @”JUMP”;
jump.position = CGPointMake(200, 20);
[self addChild:jump];
SKLabelNode *shoot = [SKLabelNode node];
shoot.name = @”shoot”;
shoot.text = @”SHOOT”;
shoot.position = CGPointMake(350, 20);
[self addChild:shoot];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *jump = [self childNodeWithName:@”jump”];
SKNode *shoot = [self childNodeWithName:@”shoot”];
SKNode *player = [self childNodeWithName:@”player”];
if ([jump containsPoint:p]) {
[player.physicsBody applyImpulse:CGVectorMake(0, 20)];
} else if ([shoot containsPoint:p]) {
SKNode *ball = [player childNodeWithName:@”ball”];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];
[ball.physicsBody applyImpulse:CGVectorMake(20, 20)];
[ball performSelector:@selector(removeFromParent) withObject:nil afterDelay:2.0];
[self performSelector:@selector(createBall) withObject:nil afterDelay:1.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 = [[ThrowScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end