ウニみたいなトゲトゲをピョンピョンさせるiPhoneアプリのサンプルコードを描いてみます。
動画でサンプルを確認
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface EchinusScene : SKScene
@property BOOL contentCreated;
@end
@implementation EchinusScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.backgroundColor = [SKColor colorWithHue:0.5 saturation:0.2 brightness:1.0 alpha:1.0];
[self createWall];
[self createEchinus];
}
– (void)createWall
{
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(320, 10)];
ground.position = CGPointMake(CGRectGetMidX(self.frame), 0);
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
for (int i=0; i<2; i++) {
SKSpriteNode *side = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(10, 500)];
side.position = CGPointMake(i*320, CGRectGetMidY(self.frame));
[self addChild:side];
side.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:side.size];
side.physicsBody.dynamic = NO;
}
}
– (void)createEchinus
{
UIBezierPath *bodyPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-30, –30, 60, 60)];
SKShapeNode *body = [SKShapeNode node];
body.name = @”body”;
body.fillColor = [SKColor blackColor];
body.strokeColor = [SKColor blackColor];
body.path = bodyPath.CGPath;
body.position = CGPointMake(160, 300);
[self addChild:body];
body.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
body.physicsBody.density = 0.1;
for (int i=0; i<10; i++) {
float angle = (2.0 * M_PI / 10.0) * i;
float rx = body.position.x + 60 * cos(angle);
float ry = body.position.y + 60 * sin(angle);
SKSpriteNode *foot = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(5, 5)];
foot.name = @”foot”;
foot.position = CGPointMake(rx, ry);
[self addChild:foot];
foot.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:2.5];
float ox = body.position.x + 30 * cos(angle);
float oy = body.position.y + 30 * sin(angle);
SKPhysicsJointSpring *springJoint = [SKPhysicsJointSpring jointWithBodyA:body.physicsBody bodyB:foot.physicsBody anchorA:CGPointMake(ox, oy) anchorB:foot.position];
springJoint.damping = 0.1;
springJoint.frequency = 10.5;
[self.physicsWorld addJoint:springJoint];
SKPhysicsJointSliding *slide = [SKPhysicsJointSliding jointWithBodyA:body.physicsBody bodyB:foot.physicsBody anchor:body.position axis:CGVectorMake(cos(angle), sin(angle))];
[self.physicsWorld addJoint:slide];
}
}
– (void)didSimulatePhysics
{
SKNode *body = [self childNodeWithName:@”body”];
[self enumerateChildNodesWithName:@”needle” usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
[self enumerateChildNodesWithName:@”foot” usingBlock:^(SKNode *node, BOOL *stop) {
if (!isnan(node.position.x) && !isnan(node.position.y)) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:body.position];
[path addLineToPoint:node.position];
SKShapeNode *needle = [SKShapeNode node];
needle.name = @”needle”;
needle.path = path.CGPath;
needle.fillColor = [SKColor blackColor];
needle.strokeColor = [SKColor blackColor];
needle.lineWidth = 6;
[self addChild:needle];
}
}];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *body = [self childNodeWithName:@”body”];
[body.physicsBody applyImpulse:CGVectorMake(0, 10.0)];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[EchinusScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end