歯でばい菌を退治する感じでiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface TeethScene : SKScene <SKPhysicsContactDelegate>
@property (nonatomic, strong) NSMutableArray *teeth;
@end
@implementation TeethScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor blackColor];
self.physicsWorld.contactDelegate = self;
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createTooth];
[self createLip];
[self createButton];
}
– (void)createTooth
{
self.teeth = [NSMutableArray array];
for (int i=0; i<20; i++) {
float w = CGRectGetMaxX(self.view.bounds) / 12;
float x = (i % 10) * w * 1.1 + w;
float y = (i / 10) * 230 + 50;
SKSpriteNode *t = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:0.95 alpha:1] size:CGSizeMake(w, w)];
t.position = CGPointMake(x, y);
t.name = @”tooth”;
[self addChild:t];
t.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:t.size];
t.physicsBody.dynamic = NO;
t.physicsBody.categoryBitMask = 0x1 << 1;
t.physicsBody.contactTestBitMask = 0x1 << 2;
[self.teeth addObject:t];
}
}
– (void)createLip
{
SKSpriteNode *bottom = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:1 green:0.6 blue:0.6 alpha:1] size:CGSizeMake(CGRectGetMaxX(self.frame), 30)];
bottom.position= CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 10);
[self addChild:bottom];
SKSpriteNode *top = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:1 green:0.5 blue:0.5 alpha:1] size:CGSizeMake(CGRectGetMaxX(self.frame), 30)];
top.position= CGPointMake(CGRectGetMidX(self.frame), 15);
[self addChild:top];
}
– (void)createButton
{
SKLabelNode *startBtn = [SKLabelNode node];
startBtn.text = @”start”;
startBtn.name = @”start”;
startBtn.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:startBtn];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *btn = [self childNodeWithName:@”start”];
if ([btn containsPoint:p]) {
[self gameStart];
return;
}
for (int i=0; i<self.teeth.count; i++) {
SKNode *t = self.teeth[i];
if ([t containsPoint:p]) {
SKNode *top;
SKNode *bottom;
if (i < 10) {
top = t;
bottom = self.teeth[i + 10];
} else {
top = self.teeth[i – 10];
bottom = t;
}
top.physicsBody.dynamic = YES;
[top.physicsBody applyImpulse:CGVectorMake(0, 20)];
bottom.physicsBody.dynamic = YES;
[bottom.physicsBody applyImpulse:CGVectorMake(0, –20)];
}
}
}
– (void)gameStart
{
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createGerm) userInfo:nil repeats:YES];
SKNode *btn = [self childNodeWithName:@”start”];
[btn runAction:[SKAction fadeOutWithDuration:0.5] completion:^{
[btn removeFromParent];
}];
}
– (void)createGerm
{
UIBezierPath *big = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2.0*M_PI clockwise:YES];
SKShapeNode *germ = [SKShapeNode node];
germ.name = @”germ”;
germ.path = big.CGPath;
germ.position = CGPointMake(-20, CGRectGetMidY(self.frame));
germ.fillColor = [SKColor grayColor];
germ.strokeColor = [SKColor grayColor];
[self addChild:germ];
UIBezierPath *small = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:3 startAngle:0 endAngle:2.0*M_PI clockwise:YES];
for (int i=0; i<2; i++) {
SKShapeNode *eye = [SKShapeNode node];
eye.path = small.CGPath;
eye.position = CGPointMake(-10 + i * 20, 0);
eye.fillColor = [SKColor whiteColor];
[germ addChild:eye];
}
UIBezierPath *zigzag = [UIBezierPath bezierPath];
[zigzag moveToPoint:CGPointMake(0, –5)];
for (int i=1; i<7; i++) {
[zigzag addLineToPoint:CGPointMake(i*4, –5 + 3*(i%2))];
}
SKShapeNode *m = [SKShapeNode node];
m.position = CGPointMake(-12, –7);
m.path = zigzag.CGPath;
m.fillColor = [SKColor clearColor];
m.strokeColor = [SKColor whiteColor];
[germ addChild:m];
germ.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
germ.physicsBody.velocity = CGVectorMake(50.0, 0);
germ.physicsBody.linearDamping = 0;
germ.physicsBody.categoryBitMask = 0x1 << 2;
}
– (void)didBeginContact:(SKPhysicsContact *)contact
{
[contact.bodyA.node runAction:[SKAction fadeOutWithDuration:0.5] completion:^{
[contact.bodyA.node removeFromParent];
}];
[contact.bodyB.node runAction:[SKAction fadeOutWithDuration:0.5] completion:^{
[contact.bodyB.node removeFromParent];
}];
}
– (void)didSimulatePhysics
{
[self enumerateChildNodesWithName:@”tooth” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.x > 550) {
[node removeFromParent];
}
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[TeethScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end