
ピッチングマシーンが玉を飛ばすだけのiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface PitchingScene : SKScene
@end
@implementation PitchingScene
– (void)didMoveToView:(SKView *)view
{
[self createMachine];
}
– (void)createMachine
{
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(10, 100)];
body.position = CGPointMake(60, 50);
[self addChild:body];
body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
body.physicsBody.dynamic = NO;
body.physicsBody.collisionBitMask = 0x2;
body.physicsBody.categoryBitMask = 0x2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2*M_PI clockwise:NO];
for (int i=0; i<2; i++) {
SKShapeNode *tire = [SKShapeNode node];
tire.name = [NSString stringWithFormat:@”tire%d”, i];
tire.path = path.CGPath;
tire.position = CGPointMake(body.position.x, 52.5 * i + 40);
tire.fillColor = [[SKColor whiteColor] colorWithAlphaComponent:0.8];
[self addChild:tire];
tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
tire.physicsBody.friction = 1.0;
tire.physicsBody.categoryBitMask = 0x1;
tire.physicsBody.collisionBitMask = 0x1;
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:tire.physicsBody bodyB:body.physicsBody anchor:tire.position];
[self.physicsWorld addJoint:pin];
}
UIBezierPath *guidePath = [UIBezierPath bezierPath];
[guidePath moveToPoint:CGPointMake(-20, 50)];
[guidePath addLineToPoint:CGPointMake(-20, –20)];
[guidePath addArcWithCenter:CGPointMake(0, –20) radius:20 startAngle:M_PI endAngle:3.0*M_PI_2 clockwise:YES];
SKShapeNode *guide = [SKShapeNode node];
guide.position = CGPointMake(30, 100);
guide.path = guidePath.CGPath;
guide.lineWidth = 3;
SKSpriteNode *guideB = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(4, 60)];
guideB.position = CGPointMake(30, 120);
[self addChild:guideB];
guideB.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:guideB.size];
guideB.physicsBody.dynamic = NO;
guide.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:guidePath.CGPath];
[self addChild:guide];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self createBall];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *tireOne = [self childNodeWithName:@”tire0″];
tireOne.physicsBody.angularVelocity = –200.0;
SKNode *tireTwo = [self childNodeWithName:@”tire1″];
tireTwo.physicsBody.angularVelocity = 200.0;
}
– (void)createBall
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:9 startAngle:0 endAngle:2*M_PI clockwise:NO];
SKShapeNode *ball = [SKShapeNode node];
ball.path = path.CGPath;
ball.position = CGPointMake(20, 150);
ball.fillColor = [SKColor whiteColor];
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:9];
ball.physicsBody.categoryBitMask = 0x1;
ball.physicsBody.collisionBitMask = 0x1;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[PitchingScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end