レコード盤に出てくる小さい四角を消していくiPhoneアプリのサンプルコード
#import “ViewController.h”
@import SpriteKit;
@interface RecordScene : SKScene
@end
@implementation RecordScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor brownColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[self createDisk];
[self createCover];
[self createPlayer];
[self createButtons];
}
– (void)createDisk
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:140 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *disk = [SKShapeNode node];
disk.name = @”disk”;
disk.position = CGPointMake(160, 300);
disk.fillColor = [SKColor colorWithWhite:0.1 alpha:1];
disk.strokeColor = [SKColor darkGrayColor];
disk.path = path.CGPath;
[self addChild:disk];
disk.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:140];
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:self.physicsBody bodyB:disk.physicsBody anchor:disk.position];
[self.physicsWorld addJoint:pin];
for (int i=0; i<3; i++) {
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:110-i*30 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *l = [SKShapeNode node];
l.strokeColor = [SKColor darkGrayColor];
l.path = path.CGPath;
[disk addChild:l];
}
}
– (void)createCover
{
SKNode *disk = [self childNodeWithName:@”disk”];
SKSpriteNode *cover = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(140, 140)];
cover.position = CGPointMake(disk.position.x + 80, disk.position.y + 80);
cover.zPosition = 100;
[self addChild:cover];
}
– (void)createPlayer
{
SKNode *disk = [self childNodeWithName:@”disk”];
SKSpriteNode *player =[SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(20, 20)];
player.name = @”player”;
player.position = CGPointMake(disk.position.x – 80, disk.position.y);
player.zRotation = M_PI/4.0;
[self addChild:player];
}
– (void)createButtons
{
for (int i=0; i<2; i++) {
SKLabelNode *l = [SKLabelNode node];
l.text = i ? @”L” : @”R”;
l.name = l.text;
l.position = CGPointMake(160 + (i ? –60 : 60), 50);
[self addChild:l];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *l = [self childNodeWithName:@”L”];
SKNode *r = [self childNodeWithName:@”R”];
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *player = [self childNodeWithName:@”player”];
if ([l containsPoint:p]) {
[player runAction:[SKAction moveByX:-30 y:0 duration:0.3]];
} else if ([r containsPoint:p]) {
[player runAction:[SKAction moveByX:30 y:0 duration:0.3]];
}
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *disk = [self childNodeWithName:@”disk”];
disk.physicsBody.angularVelocity = –1;
}
– (void)didSimulatePhysics
{
SKNode *player = [self childNodeWithName:@”player”];
[self enumerateChildNodesWithName:@”trap” usingBlock:^(SKNode *node, BOOL *stop) {
if ([player containsPoint:node.position]) {
SKAction *rotation = [SKAction rotateByAngle:2.0 *M_PI duration:0.5];
[player runAction:rotation];
[node runAction:[SKAction fadeOutWithDuration:0.3] completion:^{
[node removeFromParent];
}];
}
}];
int n =arc4random() % 200;
if (n < 4) {
SKNode *disk = [self childNodeWithName:@”disk”];
SKSpriteNode *trap = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(5, 5)];
trap.name = @”trap”;
trap.position = CGPointMake(disk.position.x + 30 * n + 50, disk.position.y);
[self addChild:trap];
trap.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:trap.size];
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:trap.physicsBody bodyB:disk.physicsBody anchor:trap.position];
[self.physicsWorld addJoint:pin];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[RecordScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end