ボールを弾ませて、上手くゴミ箱にいれてみようといった感じでiPhoneアプリのサンプルコードを描いてみます。
今回使った画像
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface DustScene : SKScene
@end
@implementation DustScene
– (void)didMoveToView:(SKView *)view
{
[self createSceneContents];
}
– (void)createSceneContents
{
[self createBoard];
[self createBlocks];
[self createDustBox];
[self createDustBall];
}
– (void)createBoard
{
SKSpriteNode *board = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:self.size];
board.name = @”board”;
board.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:board];
board.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:board.size];
board.physicsBody.dynamic = NO;
board.physicsBody.categoryBitMask = 0x1 << 2;
board.physicsBody.collisionBitMask = 0x1 << 3;
}
– (void)createBlocks
{
SKNode *board = [self childNodeWithName:@”board”];
for (int i=0; i<10; i++) {
float x = i * 50 + 25;
float y = 50;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-24, –24, 48, 48) cornerRadius:3];
SKShapeNode *block = [SKShapeNode node];
block.name = @”block”;
block.path = path.CGPath;
block.position = CGPointMake(x, y);
block.fillColor = [SKColor brownColor];
[self addChild:block];
block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(45, 45)];
block.physicsBody.categoryBitMask = 0x1 << 1;
block.physicsBody.collisionBitMask = 0x1 << 1;
block.physicsBody.allowsRotation = NO;
SKPhysicsJointLimit *s1 = [SKPhysicsJointLimit jointWithBodyA:board.physicsBody bodyB:block.physicsBody anchorA:block.position anchorB:block.position];
s1.maxLength = 20;
[self.physicsWorld addJoint:s1];
}
}
– (void)createDustBox
{
SKNode *board = [self childNodeWithName:@”board”];
SKSpriteNode *cover = [SKSpriteNode spriteNodeWithImageNamed:@”dustcover”];
cover.name = @”cover”;
cover.position = CGPointMake(CGRectGetMidX(self.frame), 200);
[self addChild:cover];
cover.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cover.size];
cover.physicsBody.categoryBitMask = 0x1 << 1;
cover.physicsBody.collisionBitMask = 0x1 << 1;
SKPhysicsJointSliding *sliding = [SKPhysicsJointSliding jointWithBodyA:cover.physicsBody bodyB:board.physicsBody anchor:cover.position axis:CGVectorMake(0, 1)];
[self.physicsWorld addJoint:sliding];
SKSpriteNode *box = [SKSpriteNode spriteNodeWithImageNamed:@”dustbox”];
box.position = CGPointMake(CGRectGetMidX(self.frame), 120);
[self addChild:box];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
box.physicsBody.categoryBitMask = 0x1 << 1;
box.physicsBody.collisionBitMask = 0x1 << 1;
}
– (void)createDustBall
{
float x = arc4random() % 100 + 20;
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@”dust”];
ball.name = @”ball”;
ball.position = CGPointMake(x, 450);
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2.0];
ball.physicsBody.restitution = 0.3;
ball.physicsBody.density = 0.1;
ball.physicsBody.categoryBitMask = 0x1 << 1;
ball.physicsBody.collisionBitMask = 0x1 << 1;
}
– (void)didSimulatePhysics
{
SKNode *ball = [self childNodeWithName:@”ball”];
if (ball.position.y < 0) {
[ball removeFromParent];
[self createDustBall];
} else {
ball.physicsBody.angularVelocity = –5;
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *cover = [self childNodeWithName:@”cover”];
[self enumerateChildNodesWithName:@”block” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
[node.physicsBody applyImpulse:CGVectorMake(0, 50)];
[cover.physicsBody applyImpulse:CGVectorMake(0, 150)];
}
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[DustScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end