攻めと守りをスイッチで切りかえながら的に弾を当てていくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
#define ButtonLeft 1
#define ButtonRight 2
@interface SwitchingScene : SKScene
@property (nonatomic) int selectButtonType;
@property (nonatomic, weak) SKNode *currentCharacter;
@property (nonatomic) NSUInteger counter;
@end
@implementation SwitchingScene
– (void)didMoveToView:(SKView *)view
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createCharacters];
[self createButtons];
[self createEnemy];
}
– (void)createCharacters
{
SKSpriteNode *sheild = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 10)];
sheild.name = @”sheild”;
sheild.position = CGPointMake(160, 200);
[self addChild:sheild];
sheild.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sheild.size];
sheild.physicsBody.density = 100;
sheild.physicsBody.linearDamping = 1.0;
sheild.physicsBody.allowsRotation = NO;
SKNode *fighter = [SKNode node];
fighter.name = @”fighter”;
fighter.position = CGPointMake(160, 150);
[self addChild:fighter];
SKSpriteNode *bottom = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(40, 20)];
[fighter addChild:bottom];
SKSpriteNode *top = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(15, 30)];
top.position = CGPointMake(0, 10);
[fighter addChild:top];
fighter.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bottom.size];
self.currentCharacter = fighter;
}
– (void)createButtons
{
SKSpriteNode *switchSheild = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(30, 20)];
switchSheild.position = CGPointMake(140, 50);
switchSheild.name = @”switchSheild”;
[self addChild:switchSheild];
SKSpriteNode *switchFighter = [SKSpriteNode spriteNodeWithColor:[UIColor grayColor] size:CGSizeMake(30, 20)];
switchFighter.position = CGPointMake(180, 50);
switchFighter.name = @”switchFighter”;
[self addChild:switchFighter];
UIBezierPath *triangle = [UIBezierPath bezierPath];
float r = 20;
[triangle moveToPoint:CGPointMake(r * cos(M_PI/1.5), r * sin(M_PI/1.5))];
[triangle addLineToPoint:CGPointMake(r * cos(2.0 * M_PI/1.5), r * sin(2.0 * M_PI/1.5))];
[triangle addLineToPoint:CGPointMake(r * cos(3.0 * M_PI/1.5), r * sin(3.0 * M_PI/1.5))];
[triangle closePath];
SKShapeNode *left = [SKShapeNode node];
left.name = @”left”;
left.position = CGPointMake(90, 50);
left.path = triangle.CGPath;
left.fillColor = [SKColor colorWithWhite:0.95 alpha:1];
left.zRotation = M_PI;
[self addChild:left];
SKShapeNode *right = [SKShapeNode node];
right.name = @”right”;
right.position = CGPointMake(230, 50);
right.path = triangle.CGPath;
right.fillColor = [SKColor colorWithWhite:0.95 alpha:1];
[self addChild:right];
}
– (void)createEnemy
{
SKSpriteNode *enemy = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(40, 40)];
enemy.name = @”enemy”;
enemy.position = CGPointMake(210, CGRectGetMaxY(self.frame) – 80);
[self addChild:enemy];
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size];
enemy.physicsBody.linearDamping = 1.0;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *right = [self childNodeWithName:@”right”];
SKNode *left = [self childNodeWithName:@”left”];
SKNode *switchSheild = [self childNodeWithName:@”switchSheild”];
SKNode *switchFighter = [self childNodeWithName:@”switchFighter”];
if ([right containsPoint:p]) {
self.selectButtonType = ButtonRight;
} else if ([left containsPoint:p]) {
self.selectButtonType = ButtonLeft;
} else if ([switchSheild containsPoint:p]) {
self.currentCharacter = [self childNodeWithName:@”sheild”];
} else if ([switchFighter containsPoint:p]) {
self.currentCharacter = [self childNodeWithName:@”fighter”];
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.selectButtonType = 0;
}
– (void)update:(NSTimeInterval)currentTime
{
float dx = 0;
SKNode *fighter =[self childNodeWithName:@”fighter”];
SKNode *sheild =[self childNodeWithName:@”sheild”];
SKNode *enemy = [self childNodeWithName:@”enemy”];
if (self.currentCharacter == fighter) {
dx = 30;
} else {
dx = 100;
}
if (self.selectButtonType == ButtonRight) {
self.currentCharacter.physicsBody.velocity = CGVectorMake(dx, 0);
} else if (self.selectButtonType == ButtonLeft) {
self.currentCharacter.physicsBody.velocity = CGVectorMake(-dx, 0);
} else {
self.currentCharacter.physicsBody.velocity = CGVectorMake(0, 0);
}
self.counter = (self.counter + 1) % 1000;
if (self.counter % 100 == 0) {
// shoot from fighter.
if (![sheild containsPoint:CGPointMake(fighter.position.x, sheild.position.y)]) {
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5, 5)];
[self addChild:n];
n.position = CGPointMake(fighter.position.x, fighter.position.y + 30);
n.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n.size];
[n.physicsBody applyImpulse:CGVectorMake(0, 1)];
[n runAction:[SKAction sequence:@[[SKAction waitForDuration:1.0], [SKAction runBlock:^{
[n removeFromParent];
}]]]];
}
} if (self.counter % 300 == 0) {
for (int i=0; i<15; i++) {
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10, 10)];
[self addChild:n];
n.position = CGPointMake(enemy.position.x, enemy.position.y – 40);
n.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n.size];
[n.physicsBody applyImpulse:CGVectorMake((arc4random() % 10) * 0.2 – 1.0 , –1.0)];
[n runAction:[SKAction sequence:@[[SKAction waitForDuration:3.0], [SKAction runBlock:^{
[n removeFromParent];
}]]]];
}
}
if (fighter.zRotation != 0) {
fighter.name = @””;
[fighter runAction:[SKAction fadeOutWithDuration:1.0] completion:^{
[fighter removeFromParent];
[sheild removeFromParent];
[self createCharacters];
}];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[SwitchingScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end