ヨーヨーっぽい動きをする丸をビヨンビヨンさせてあそぶiPhoneアプリのサンプルコードを描いてみます。
動かすとこんな感じ
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface YoYoScene : SKScene
@property (nonatomic, weak) SKNode *selected;
@property CGPoint yoyoConnectPoint;
@end
@implementation YoYoScene
-(void)didMoveToView:(SKView *)view
{
[self createSceneContents];
}
– (void)createSceneContents
{
[self createWall];
[self createYoYo];
[self createRing];
[self createJoint];
}
– (void)createWall
{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.frame];
SKShapeNode *ground = [SKShapeNode node];
ground.lineWidth = 5;
ground.path = path.CGPath;
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:path.CGPath];
}
– (void)createYoYo
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-30, –30, 60, 60)];
SKShapeNode *yoyo = [SKShapeNode node];
yoyo.name = @”yoyo”;
yoyo.zPosition = 1;
yoyo.path = path.CGPath;
yoyo.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
yoyo.fillColor = [SKColor yellowColor];
[self addChild:yoyo];
yoyo.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
SKLabelNode *logo = [SKLabelNode node];
logo.fontSize = 40;
logo.fontColor = [UIColor redColor];
logo.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
logo.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
logo.text = @”★”;
[yoyo addChild:logo];
}
– (void)createRing
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];
SKShapeNode *ring = [SKShapeNode node];
ring.name = @”ring”;
ring.path = path.CGPath;
ring.lineWidth = 8;
ring.position = CGPointMake(CGRectGetMidX(self.frame), 400);
[self addChild:ring];
ring.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
ring.physicsBody.dynamic = NO;
}
– (void)createJoint
{
SKNode *yoyo = [self childNodeWithName:@”yoyo”];
SKNode *ring = [self childNodeWithName:@”ring”];
self.yoyoConnectPoint = CGPointMake(yoyo.position.x – 30, yoyo.position.y);
SKPhysicsJointLimit *line = [SKPhysicsJointLimit jointWithBodyA:yoyo.physicsBody bodyB:ring.physicsBody anchorA:self.yoyoConnectPoint anchorB:ring.position];
[self.physicsWorld addJoint:line];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *ring = [self childNodeWithName:@”ring”];
if ([ring containsPoint:p]) {
self.selected = ring;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
if (self.selected) {
self.selected.position = p;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.selected = nil;
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *yoyo = [self childNodeWithName:@”yoyo”];
SKNode *ring = [self childNodeWithName:@”ring”];
[self.physicsWorld removeAllJoints];
float dh = 400 – ring.position.y – yoyo.position.y;
self.yoyoConnectPoint = CGPointMake(yoyo.position.x – dh/10.0, yoyo.position.y);
SKPhysicsJointLimit *limit = [SKPhysicsJointLimit jointWithBodyA:yoyo.physicsBody bodyB:ring.physicsBody anchorA:self.yoyoConnectPoint anchorB:ring.position];
[self.physicsWorld addJoint:limit];
}
– (void)didSimulatePhysics
{
SKNode *ring = [self childNodeWithName:@”ring”];
SKShapeNode *lineBoard = (SKShapeNode*)[self childNodeWithName:@”lineBoard”];
if (!lineBoard) {
lineBoard = [SKShapeNode node];
lineBoard.name = @”lineBoard”;
lineBoard.strokeColor = [UIColor lightGrayColor];
lineBoard.lineWidth = 3;
[self addChild:lineBoard];
}
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:self.yoyoConnectPoint];
[path addLineToPoint:ring.position];
lineBoard.path = path.CGPath;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[YoYoScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end