ミツバチを飛ばして、蜂蜜をあつめるiPhoneアプリのサンプルコードを描いてみます。
今回使った画像
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface HoneyScene : SKScene
@property (nonatomic, weak) SKNode *selected;
@end
@implementation HoneyScene
– (void)didMoveToView:(SKView *)view
{
[self createSceneContents];
}
– (void)createSceneContents
{
self.physicsWorld.speed = 0.6;
self.backgroundColor = [UIColor brownColor];
[self createGarden];
[self createHoneyCombed];
[self createBee];
}
– (void)createGarden
{
// wall
SKSpriteNode *wall = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:self.frame.size];
wall.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:wall.frame];
wall.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:wall];
// grass
SKSpriteNode *grass = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 20)];
grass.position = CGPointMake(CGRectGetMidX(self.frame), 10);
[self addChild:grass];
grass.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:grass.frame.size];
grass.physicsBody.dynamic = NO;
// flower
for (int i=0; i<3; i++) {
SKSpriteNode *stem = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(5, 50)];
stem.position = CGPointMake(i * 80 + 250, 25);
[self addChild:stem];
SKSpriteNode *flower = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(40, 40)];
flower.name = @”flower”;
flower.position = CGPointMake(i * 80 + 250, 70);
[self addChild:flower];
SKSpriteNode *flower45 = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(40, 40)];
flower45.position = flower.position;
flower45.zRotation = M_PI / 4.0;
[self addChild:flower45];
}
}
– (void)createHoneyCombed
{
CGPoint o = CGPointMake(400, 240);
[self createHexAtPoint:o];
for (int i=0; i<10; i++) {
float angle = i * M_PI / 3.0 + M_PI/6.0;
float x = 35 * cos(angle) + o.x;
float y = 35 * sin(angle) + o.y;
[self createHexAtPoint:CGPointMake(x, y)];
}
}
– (void)createHexAtPoint:(CGPoint)p
{
float r = 20;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(r * cos(0), r * sin(0))];
for (int i=1; i<6; i++) {
float angle = i * M_PI / 3.0;
[path addLineToPoint:CGPointMake(r * cos(angle), r * sin(angle))];
}
[path closePath];
SKShapeNode *hex = [SKShapeNode node];
hex.name = @”honeycombed”;
hex.path = path.CGPath;
hex.position = p;
[self addChild:hex];
}
– (void)createBee
{
SKSpriteNode *bee = [SKSpriteNode spriteNodeWithImageNamed:@”bee”];
bee.name = @”bee”;
bee.position = CGPointMake(80, 150);
[self addChild:bee];
bee.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:bee.size.width*0.5];
bee.physicsBody.restitution = 0.9;
bee.physicsBody.density = 0.3;
bee.physicsBody.dynamic = NO;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *bee = [self childNodeWithName:@”bee”];
if ([bee containsPoint:p]) {
self.selected = bee;
// create shadow
SKSpriteNode *beeShadow = [bee copy];
beeShadow.name = @”shadow”;
beeShadow.alpha = 0.4;
[self addChild:beeShadow];
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
if (self.selected) {
self.selected.position = p;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.selected) {
SKNode *bee = [self childNodeWithName:@”bee”];
SKNode *shadow = [self childNodeWithName:@”shadow”];
bee.physicsBody.dynamic = YES;
float dx = MIN(shadow.position.x – bee.position.x, 80);
float dy = MIN(shadow.position.y – bee.position.y, 80);
CGVector v = CGVectorMake(dx, dy);
[bee.physicsBody applyImpulse:v];
bee.name = @”beeee”;
[shadow removeFromParent];
self.selected = nil;
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self createBee];
});
}
}
– (void)update:(NSTimeInterval)currentTime
{
// honey
NSPredicate *flowersPred = [NSPredicate predicateWithFormat:@”name == %@”, @”flower”];
NSArray *flowers = [self.children filteredArrayUsingPredicate:flowersPred];
[self enumerateChildNodesWithName:@”beeee” usingBlock:^(SKNode *node, BOOL *stop) {
for (SKNode *flower in flowers) {
if ([node intersectsNode:flower]) {
node.name = @”beeWithHoney”;
SKSpriteNode *honey = [SKSpriteNode spriteNodeWithImageNamed:@”honey”];
honey.name = @”honey”;
honey.position = CGPointMake(10, –40);
[node addChild:honey];
}
}
}];
NSPredicate *beePred = [NSPredicate predicateWithFormat:@”name == %@”, @”beeWithHoney”];
NSArray *bees = [self.children filteredArrayUsingPredicate:beePred];
// honeycombed
[self enumerateChildNodesWithName:@”honeycombed” usingBlock:^(SKNode *node, BOOL *stop) {
for (SKNode *beeWithHoney in bees) {
if ([beeWithHoney intersectsNode:node]) {
((SKShapeNode*)node).fillColor = [[SKColor yellowColor] colorWithAlphaComponent:0.7];
[[beeWithHoney childNodeWithName:@”honey”] removeFromParent];
beeWithHoney.name = @”beeee”;
node.name = @”honeycombedWithHoney”;
break;
}
}
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[HoneyScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end