iPhoneひよこえさ

ひよこっぽいのに餌を打ちまくるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface PiyoScene : SKScene

@end

@implementation PiyoScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    [self createPiyo];

    [self createFoodGun];

}

– (void)createPiyo

{

    SKNode *piyo = [SKNode node];

    piyo.position = CGPointMake(CGRectGetMaxX(self.frame)-80, 100);

    [self addChild:piyo];

    

    SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(80, 120)];

    [piyo addChild:body];

    

    SKSpriteNode *eye = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(10, 10)];

    eye.position = CGPointMake(-20, 40);

    [piyo addChild:eye];

    

    SKSpriteNode *beakA = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(30, 10)];

    beakA.position = CGPointMake(-45, 25);

    [piyo addChild:beakA];

    

    SKSpriteNode *beakB = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(30, 10)];

    beakB.position = CGPointMake(-45, 0);

    [piyo addChild:beakB];

    for (int i=0; i<2; i++) {

        SKSpriteNode *f = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(7, 15)];

        f.position = CGPointMake(i==0 ? –15 : 10 , –70);

        [piyo addChild:f];

    }

    

    NSMutableArray *pBodies = [NSMutableArray array];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:beakA.size center:beakA.position]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:beakB.size center:beakB.position]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 35) center:CGPointMake(-35, 42.5)]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(80, 5) center:CGPointMake(0, 60)]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 120) center:CGPointMake(35, 0)]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(80, 5) center:CGPointMake(0, –60)]];

    [pBodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 60) center:CGPointMake(-35, –20)]];

    

    piyo.physicsBody =[SKPhysicsBody bodyWithBodies:pBodies];

    piyo.physicsBody.allowsRotation = NO;

    piyo.physicsBody.linearDamping = 1.0;

    

    SKAction *jump = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction waitForDuration:1.0], [SKAction runBlock:^{

        [piyo.physicsBody applyImpulse:CGVectorMake(0, 80)];

    }]]]];

    [piyo runAction:jump];

}

– (void)createFoodGun

{

    SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(50, 50)];

    n.name = @”button”;

    n.position = CGPointMake(25, 25);

    [self addChild:n];

    

    SKSpriteNode *gun = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(60, 10)];

    gun.name = @”gun”;

    gun.position = CGPointMake(70, 45);

    [self addChild:gun];

    

    n.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n.size];

    n.physicsBody.dynamic = NO;

    

    gun.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:gun.size];

    gun.physicsBody.angularDamping = 1.0;

    

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:n.physicsBody bodyB:gun.physicsBody anchor:CGPointMake(45, 45)];

    [self.physicsWorld addJoint:pin];

    pin.shouldEnableLimits = YES;

    pin.lowerAngleLimit = –0.2;

    pin.upperAngleLimit = 0.8;

    

    SKAction *jump = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction waitForDuration:0.5], [SKAction runBlock:^{

        [gun.physicsBody applyAngularImpulse:0.015];

    }]]]];

    [gun runAction:jump];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SKNode *gun = [self childNodeWithName:@”gun”];

    SKSpriteNode *food = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(6, 6)];

    CGPoint p1 = [gun convertPoint:CGPointMake(45, 5) toNode:self];

    CGPoint p2 = [gun convertPoint:CGPointMake(0, 5) toNode:self];

    food.position = p1;

    [self addChild:food];

    

    float dx = (p1.x – p2.x) / 400.0;

    float dy = (p1.y – p2.y) / 400.0;

    food.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];

    [food.physicsBody applyImpulse:CGVectorMake(dx, dy)];

}

@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 = [[PiyoScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end