
タップしてスイカ割りという感じでiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface SuikaScene : SKScene
@property (nonatomic) NSInteger count;
– (void)start;
@end
@implementation SuikaScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor brownColor];
[self createAdviceMarks];
[self createWaterMelon];
[self createCover];
}
– (void)createAdviceMarks
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
for (int i=0; i<4; i++) {
SKShapeNode *mark = [SKShapeNode node];
mark.name = [NSString stringWithFormat:@”mark%d”, i];
mark.path = path.CGPath;
mark.lineWidth = 2;
mark.strokeColor = [SKColor yellowColor];
mark.fillColor = [SKColor whiteColor];
mark.zPosition = 10;
float x = 140 * cos(M_PI_2 * i) + 160;
float y = 140 * sin(M_PI_2 * i) + 160;
mark.position = CGPointMake(x, y);
[self addChild:mark];
}
}
– (void)createWaterMelon
{
SKSpriteNode *melon = [SKSpriteNode spriteNodeWithImageNamed:@”watermelon”];
melon.name = @”watermelon”;
melon.position = CGPointMake(160, 160);
[self addChild:melon];
}
– (void)createCover
{
SKSpriteNode *cover = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(320, 320)];
cover.name = @”cover”;
cover.position = CGPointMake(160 + 320, 160);
[self addChild:cover];
}
– (void)start
{
SKNode *cover = [self childNodeWithName:@”cover”];
SKAction *over = [SKAction moveTo:CGPointMake(160, 160) duration:0.5];
[cover runAction:over completion:^{
SKNode *melon = [self childNodeWithName:@”watermelon”];
float x = 80 + arc4random() % 160;
float y = 80 + arc4random() % 160;
melon.position = CGPointMake(x, y);
}];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *melon = [self childNodeWithName:@”watermelon”];
SKNode *cover = [self childNodeWithName:@”cover”];
if ([melon containsPoint:p]) {
// hit!!
CGPoint mp = melon.position;
[melon removeFromParent];
SKSpriteNode *melonsplit = [SKSpriteNode spriteNodeWithImageNamed:@”watermelon_split”];
melonsplit.position = mp;
[self addChild:melonsplit];
SKAction *uncover = [SKAction moveTo:CGPointMake(-160, 160) duration:0.5];
[cover runAction:uncover];
} else {
// check left, right, top ,bottom
float dx = p.x – melon.position.x;
float dy = p.y – melon.position.y;
SKNode *mark;
if (fabs(dx) > fabs(dy)) {
if (dx > 0)
mark = [self childNodeWithName:@”mark2″];
else
mark = [self childNodeWithName:@”mark0″];
} else {
if (dy > 0)
mark = [self childNodeWithName:@”mark3″];
else
mark = [self childNodeWithName:@”mark1″];
}
UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:14 startAngle:self.count * M_PI_2 endAngle:(self.count + 1) * M_PI_2 clockwise:YES];
[arcPath addLineToPoint:CGPointZero];
[arcPath closePath];
SKShapeNode *arc = [SKShapeNode node];
arc.path = arcPath.CGPath;
arc.lineWidth = 0;
arc.fillColor = [SKColor colorWithHue:0.2 * self.count saturation:0.9 brightness:0.8 alpha:1];
[mark addChild:arc];
self.count += 1;
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
spriteView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:spriteView];
SKScene *scene = [[SuikaScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-0-[v(320)]” options:0 metrics:nil views:@{@”v”:spriteView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-50-[v(320)]” options:0 metrics:nil views:@{@”v”:spriteView}]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@”start” forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:40];
[btn sizeToFit];
btn.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:btn];
[btn addTarget:scene action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-80-[btn(160)]” options:0 metrics:nil views:@{@”btn”:btn}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn(40)]-50-|” options:0 metrics:nil views:@{@”btn”:btn}]];
}
@end