iPhoneとんがり

とんがった所にブーメランをのっけるといったiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じ

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface PointedScene : SKScene

@end

@implementation PointedScene

– (void)didMoveToView:(SKView *)view

{

    [self createSceneContents];

}

– (void)createSceneContents

{

    self.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:1.0];

    [self createGround];

    [self createCone];

    [self createBoomerang];

}

– (void)createGround

{

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 50)];

    ground.position = CGPointMake(CGRectGetMidX(self.frame), 25);

    [self addChild:ground];

    

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

    ground.physicsBody.dynamic = NO;

}

– (void)createCone

{

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

        UIBezierPath *path = [UIBezierPath bezierPath];

        float s = 60;

        [path moveToPoint:CGPointMake(0, 0)];

        [path addLineToPoint:CGPointMake(s, 0)];

        [path addLineToPoint:CGPointMake(s/2.0, s)];

        [path closePath];

        

        SKShapeNode *cone = [SKShapeNode node];

        cone.path = path.CGPath;

        cone.fillColor = [SKColor redColor];

        cone.position = CGPointMake(400 – i*130, 70);

        [self addChild:cone];

        

        cone.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];

        cone.physicsBody.dynamic = NO;

    }

}

– (void)createBoomerang

{

    SKNode *boomerang = [SKNode node];

    boomerang.name = @”boomerang”;

    [self addChild:boomerang];

    

    SKSpriteNode *blageA = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(50, 10)];

    blageA.position = CGPointMake(80, 100);

    [boomerang addChild:blageA];

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

    

    SKSpriteNode *blageB = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(10, 50)];

    blageB.position = CGPointMake(100, 80);

    [boomerang addChild:blageB];

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

    

    SKPhysicsJointFixed *fixed = [SKPhysicsJointFixed jointWithBodyA:blageA.physicsBody bodyB:blageB.physicsBody anchor:CGPointMake(105, 100)];

    [self.physicsWorld addJoint:fixed];

}

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

{

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

    SKNode *blade = boomerang.children[0];

    blade.physicsBody.velocity = CGVectorMake(150, 1000);

    blade.physicsBody.angularVelocity = –50;

    

}

– (void)didSimulatePhysics

{

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

    SKNode *blade = boomerang.children[0];

    if (blade.position.x > CGRectGetMaxX(self.frame) || blade.position.x <  0) {

        [boomerang removeFromParent];

        [self createBoomerang];

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[PointedScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end