円の中にある長方形でボールを押し出すiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createCircle];
[self createBar];
[self createBall];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *scene = [SKScene sceneWithSize:sv.bounds.size];
scene.backgroundColor = [self color:0];
[sv presentScene:scene];
[self.view addSubview:sv];
self.scene = scene;
}
– (void)createCircle {
float r = CGRectGetMaxX(self.view.bounds) * 0.3;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(r * cos(1.8 * M_PI), r * sin(1.8 * M_PI))];
[path addArcWithCenter:CGPointZero radius:r startAngle:1.8 * M_PI endAngle:0 clockwise:NO];
[path addLineToPoint:CGPointMake(r*0.95, 0)];
[path addArcWithCenter:CGPointZero radius:r*0.95 startAngle:0 endAngle:1.8 * M_PI clockwise:YES];
[path closePath];
SKShapeNode *circle = [SKShapeNode shapeNodeWithPath:path.CGPath];
circle.name = @”circle”;
circle.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
circle.lineWidth = 4;
circle.strokeColor = [self color:1];
[self.scene addChild:circle];
circle.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path.CGPath];
circle.physicsBody.restitution = 0.0;
circle.physicsBody.dynamic = NO;
[circle runAction:[SKAction rotateByAngle:0.9*M_PI duration:3.0]];
}
– (void)createBar {
CGPoint o = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
for (int i=0; i<9; i++) {
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(12, 50)];
bar.name = [NSString stringWithFormat:@”bar%d”, i];
bar.position = CGPointMake(20 * i – 80 + o.x, 0 + o.y);
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.dynamic = NO;
[self.scene addChild:bar];
}
}
– (void)createBall {
float r = 15;
SKShapeNode *ball = [SKShapeNode shapeNodeWithCircleOfRadius:r];
ball.name = @”ball”;
ball.position = CGPointMake(CGRectGetMaxX(self.view.bounds) * 0.3, CGRectGetMaxY(self.view.bounds) * 0.8);
ball.fillColor = [self color:1];
[self.scene addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:r];
ball.physicsBody.dynamic = NO;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *ball = [self.scene childNodeWithName:@”ball”];
SKNode *circle = [self.scene childNodeWithName:@”circle”];
ball.physicsBody.dynamic = YES;
[circle runAction:[SKAction rotateByAngle:-M_PI/1.5 duration:4.0]];
[self.scene enumerateChildNodesWithName:@”bar[0-9]” usingBlock:^(SKNode *node, BOOL *stop) {
int i = [[node.name substringFromIndex:3] intValue];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[node runAction:[SKAction moveByX:0 y:20 duration:1.0] completion:^{
[node runAction:[SKAction moveByX:0 y:-20 duration:1.0]];
}];
});
}];
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]
– (UIColor *)color:(int)i {
switch (i) {
case 0: return ColorHex(0x0F140D);
case 1: return ColorHex(0x3AFA06);
}
return nil;
}
@end