iPhoneシーソー

シーソーの上に数字書いてある四角形を落として遊ぶようなiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface SeesawScene : SKScene

@end

@implementation SeesawScene

– (void)didMoveToView:(SKView *)view

{

    [self createSceneContents];

}

– (void)createSceneContents

{

    [self createTitle];

    [self createSeesaw];

    [self start];

}

– (void)createTitle

{

    SKLabelNode *title = [SKLabelNode node];

    title.text = @”seesaw”;

    title.fontSize = 50;

    title.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 80);

    [self addChild:title];

    

    SKAction *up = [SKAction rotateByAngle:M_PI * 0.1 duration:3.0];

    SKAction *down = [SKAction rotateByAngle:-M_PI * 0.1 duration:3.0];

    SKAction *updown = [SKAction repeatActionForever:[SKAction sequence:@[up, down]]];

    

    [title runAction:updown];

}

– (void)createSeesaw

{

    float r = 30;

    float dAngle = 2.0 * M_PI / 3.0;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(r * cos(dAngle), r * sin(dAngle))];

    [path addLineToPoint:CGPointMake(r * cos(2.0 * dAngle), r * sin(2.0 * dAngle))];

    [path addLineToPoint:CGPointMake(r * cos(0), r * sin(0))];

    [path closePath];

    SKShapeNode *fulcrum = [SKShapeNode node];

    fulcrum.path = path.CGPath;

    fulcrum.position = CGPointMake(CGRectGetMidX(self.frame), 150);

    fulcrum.zRotation = M_PI/2.0;

    [self addChild:fulcrum];

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

    fulcrum.physicsBody.dynamic = NO;

    

    SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(280, 5)];

    bar.position = CGPointMake(fulcrum.position.x, fulcrum.position.y + 20);

    [self addChild:bar];

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

    bar.physicsBody.friction = 1.0;

    bar.physicsBody.restitution = 0;

    

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:bar.physicsBody bodyB:fulcrum.physicsBody anchor:bar.position];

    pin.shouldEnableLimits = YES;

    pin.upperAngleLimit = M_PI * 0.1;

    pin.lowerAngleLimit = –M_PI * 0.1;

    [self.physicsWorld addJoint:pin];

}

– (void)start

{

    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(createWeight) userInfo:Nil repeats:YES];

}

– (void)createWeight

{

    float size = arc4random() % 30 + 20;

    SKSpriteNode *weight = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(size, size)];

    weight.name = @”weight”;

    weight.position = CGPointMake(CGRectGetMaxX(self.frame) + 50, CGRectGetMaxY(self.frame) – 180);

    [self addChild:weight];

    

    SKLabelNode *weightLabel = [SKLabelNode node];

    weightLabel.text = [@((int)pow(size, 2) / 100) stringValue];

    weightLabel.fontSize = size * 0.6;

    weightLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

    [weight addChild:weightLabel];

    

    SKAction *move = [SKAction moveToX:-50 duration:10.0];

    [weight runAction:move completion:^{

        [weight removeFromParent];

    }];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    [self enumerateChildNodesWithName:@”weight” usingBlock:^(SKNode *node, BOOL *stop) {

        if ([node containsPoint:p]) {

            [node removeAllActions];

            node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:((SKSpriteNode*)node).size];

            node.physicsBody.restitution = 0;

            node.physicsBody.friction = 1.0;

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end