
丸と四角を4個のボタンを使ってストップ、ゴーで遊ぶiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface StopGoScene : SKScene
@end
@implementation StopGoScene
– (void)didMoveToView:(SKView *)view
{
[self createWall];
[self createObjects];
[self createButtons];
}
– (void)createWall
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
– (void)createObjects
{
float d = 30;
UIBezierPath *pathA = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:10 startAngle:0 endAngle:2*M_PI clockwise:NO];
UIBezierPath *pathB = [UIBezierPath bezierPathWithRect:CGRectMake(-10, –10, 20, 20)];
for (int i=0; i<100; i++) {
if ((arc4random() % 3) == 0) {
float x = (i % 10) * d + 10;
float y = (i / 10) * d + 150;
CGPathRef path = (arc4random() % 2) ? pathA.CGPath : pathB.CGPath;
SKShapeNode *object = [SKShapeNode node];
object.name = @”move object”;
object.path = path;
object.fillColor = [SKColor colorWithHue:(arc4random()%4) * 0.2 saturation:0.8 brightness:1.0 alpha:1.0];
object.position = CGPointMake(x, y);
[self addChild:object];
object.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
object.physicsBody.dynamic = NO;
}
}
}
– (void)createButtons
{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-50, –15, 100, 30) cornerRadius:10];
for (int i=0; i<4; i++) {
float x = (i % 2) * 160 + 80;
float y = (i / 2) * 50 + 40;
SKShapeNode *btn = [SKShapeNode node];
btn.path = path.CGPath;
btn.position = CGPointMake(x, y);
btn.fillColor = [SKColor colorWithHue:i * 0.2 saturation:0.8 brightness:1.0 alpha:1.0];
btn.name = @”button”;
[self addChild:btn];
SKLabelNode *l = [SKLabelNode node];
l.name = @”title”;
l.text = @”go”;
l.fontSize = 25;
l.position = CGPointMake(0, –5);
[btn addChild:l];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self enumerateChildNodesWithName:@”button” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
SKShapeNode *btn = (SKShapeNode *)node;
SKLabelNode *l = (SKLabelNode *)[btn childNodeWithName:@”title”];
if ([l.text isEqualToString:@”go”]) {
[self goColor:btn.fillColor];
l.text = @”stop”;
} else {
[self stopColor:btn.fillColor];
l.text = @”go”;
}
}
}];
}
– (void)goColor:(SKColor *)color
{
[self enumerateChildNodesWithName:@”move object” usingBlock:^(SKNode *node, BOOL *stop) {
if ([[(SKShapeNode *)node fillColor] isEqual:color]) {
// use temp body!
// workaround –> Assertion failed: (typeA == b2_dynamicBody || typeB == b2_dynamicBody)
SKPhysicsBody *temp = node.physicsBody;
temp.dynamic = YES;
node.physicsBody = temp;
}
}];
}
– (void)stopColor:(SKColor *)color
{
[self enumerateChildNodesWithName:@”move object” usingBlock:^(SKNode *node, BOOL *stop) {
if ([[(SKShapeNode *)node fillColor] isEqual:color]) {
SKPhysicsBody *temp = node.physicsBody;
temp.dynamic = NO;
node.physicsBody = temp;
}
}];
}
– (void)update:(NSTimeInterval)currentTime
{
[self enumerateChildNodesWithName:@”move object” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.physicsBody.dynamic == YES && fabs(node.physicsBody.velocity.dx) < 2.0 && fabs(node.physicsBody.velocity.dy) < 2.0) {
float dx = (arc4random() % 10) – 5;
float dy = (arc4random() % 10) – 5;
[node.physicsBody applyImpulse:CGVectorMake(dx * 0.001, dy * 0.001)];
}
}];
}
@end
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[StopGoScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
self.scene = scene;
}
@end