ボールをおっきく膨らましたり、小さくしぼませたりして、画面のはじっこまで進ませる感じのiPhoneゲームアプリのサンプルコードを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface BlowScene : SKScene
@property BOOL contentCreated;
@property BOOL big;
@property int counter;
@end
@implementation BlowScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.backgroundColor = [UIColor purpleColor];
[self createBall];
[self createMap];
}
– (void)createBall
{
UIBezierPath *ballPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];
SKShapeNode *ball = [SKShapeNode node];
ball.name = @”ball”;
ball.path = ballPath.CGPath;
ball.position = CGPointMake(20, 100);
ball.fillColor = [SKColor greenColor];
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
ball.physicsBody.restitution = 0.3;
SKLabelNode *l = [SKLabelNode node];
l.position = CGPointMake(0, 0);
l.text = @”S”;
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
[ball addChild:l];
}
– (void)createMap
{
SKSpriteNode *floorA = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(200, 10)];
floorA.position = CGPointMake(100, 50);
[self addChild:floorA];
SKSpriteNode *floorB = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(280, 10)];
floorB.position = CGPointMake(400, 50);
[self addChild:floorB];
SKSpriteNode *roof = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(100, 10)];
roof.position = CGPointMake(400, 120);
[self addChild:roof];
NSArray *objecs = @[floorA, floorB, roof];
for (SKSpriteNode *obj in objecs) {
obj.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obj.size];
obj.physicsBody.dynamic = NO;
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *ball = [self childNodeWithName:@”ball”];
if (!ball.userData) {
ball.userData = [NSMutableDictionary dictionaryWithObjectsAndKeys:@(0), @”Big”, nil];
}
NSNumber *next = @(([ball.userData[@”Big”] intValue] + 1) % 2);
[ball.userData setObject:next forKey:@”Big”];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *ball = [self childNodeWithName:@”ball”];
self.counter++;
if (self.counter % 3 == 0) {
BOOL big = [ball.userData[@”Big”] intValue] > 0;
if (big) {
if (ball.xScale == 1.0) {
SKLabelNode *l = (SKLabelNode*)ball.children[0];
l.text = @”B”;
}
if (ball.xScale < 3.0) {
ball.xScale += 0.02;
ball.yScale += 0.02;
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20.0 * ball.xScale];
}
} else {
if (ball.xScale > 3.0) {
SKLabelNode *l = (SKLabelNode*)ball.children[0];
l.text = @”S”;
}
if (ball.xScale > 1.0) {
ball.xScale -= 0.02;
ball.yScale -= 0.02;
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20.0 * ball.xScale];
}
}
}
ball.physicsBody.angularVelocity = –3.0;
}
– (void)didSimulatePhysics
{
SKNode *ball = [self childNodeWithName:@”ball”];
if (ball.position.y < –20) {
[ball removeFromParent];
[self createBall];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BlowScene alloc] initWithSize:self.view.bounds.size];
[spriteView presentScene:scene];
}
@end