鉄骨を解体してあそぶシンプルなiPhoneアプリのサンプルコードを描いてみます。
動画で見るサンプル
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface StealScene : SKScene
@property BOOL contentCreated;
@end
@implementation StealScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated= YES;
}
}
– (void)createSceneContents
{
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(320, 10)];
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
ground.position = CGPointMake(CGRectGetMidX(self.frame), 10);
[self createStealFrames];
}
– (void)createStealFrames
{
NSArray *positionAndAngle = @[
@[[NSValue valueWithCGPoint:CGPointMake(80, 200)] , @(M_PI/2.0), @”A”],
@[[NSValue valueWithCGPoint:CGPointMake(240, 200)] , @(M_PI/2.0), @”B”],
@[[NSValue valueWithCGPoint:CGPointMake(160, 280)] , @(0), @”C”],
@[[NSValue valueWithCGPoint:CGPointMake(160, 150)] , @(0), @”D”],
];
for (NSArray *arr in positionAndAngle) {
CGPoint p = [arr[0] CGPointValue];
float angle = [arr[1] floatValue];
CGSize size = CGSizeMake(200, 30);
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:size];
bar.name = arr[2];
bar.position = p;
bar.zRotation = angle;
[self addChild:bar];
SKLabelNode *label = [SKLabelNode node];
label.text = arr[2];
[bar addChild:label];
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.categoryBitMask = 0x1 << 2;
bar.physicsBody.collisionBitMask = 0x1;
// bar.physicsBody.dynamic = NO;
}
// A and C
CGPoint pinPoint = CGPointMake(80, 280);
SKNode *nodeOne = [self childNodeWithName:@”A”];
SKNode *nodeTwo = [self childNodeWithName:@”C”];
[self addPin:pinPoint nodeA:nodeOne nodeB:nodeTwo];
// A and D
pinPoint = CGPointMake(80, 150);
nodeOne = [self childNodeWithName:@”A”];
nodeTwo = [self childNodeWithName:@”D”];
[self addPin:pinPoint nodeA:nodeOne nodeB:nodeTwo];
// B and D
pinPoint = CGPointMake(240, 150);
nodeOne = [self childNodeWithName:@”B”];
nodeTwo = [self childNodeWithName:@”D”];
[self addPin:pinPoint nodeA:nodeOne nodeB:nodeTwo];
// B and C
pinPoint = CGPointMake(240, 280);
nodeOne = [self childNodeWithName:@”B”];
nodeTwo = [self childNodeWithName:@”C”];
[self addPin:pinPoint nodeA:nodeOne nodeB:nodeTwo];
}
– (void)addPin:(CGPoint)p nodeA:(SKNode*)A nodeB:(SKNode*)B
{
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:A.physicsBody bodyB:B.physicsBody anchor:p];
[self.physicsWorld addJoint:pin];
SKSpriteNode *m = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(10, 10)];
m.position = [B convertPoint:p fromNode:self];
[B addChild:m];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
NSArray *arr = [@”A B C D” componentsSeparatedByString:@” “];
for (NSString *s in arr) {
SKNode *node = [self childNodeWithName:s];
if (CGRectContainsPoint(node.frame, p)) {
[(SKSpriteNode*)node setColor:[SKColor brownColor]];
for (SKPhysicsJoint *joint in node.physicsBody.joints) {
[self.physicsWorld removeJoint:joint];
}
}
}
}
– (void)restart
{
[self removeAllChildren];
[self createSceneContents];
}
@end
@interface ViewController ()
@property (weak, nonatomic) SKView *spriteView;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
}
– (void)viewDidAppear:(BOOL)animated
{
SKView *view = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:view];
self.spriteView = view;
SKScene *scene = [[StealScene alloc] initWithSize:self.spriteView.frame.size];
[self.spriteView presentScene:scene];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@”Reset” forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[btn sizeToFit];
btn.center = CGPointMake(160, 80);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
}
– (void)reset
{
SKScene *scene = [[StealScene alloc] initWithSize:self.spriteView.frame.size];
[self.spriteView presentScene:scene transition:[SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0]];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end