iPhone鐘

今年も終わり。ということで除夜の鐘をつかって煩悩を飛ばすiPhoneアプリのサンプルコードを描いてみます。


今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BellScene : SKScene

@property BOOL contentCreated;

@end

@implementation BellScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        self.backgroundColor = [SKColor lightGrayColor];

        //[SKColor colorWithHue:225.0/360.0 saturation:1.0 brightness:0.5 alpha:1.0];

        

        [self create2013];

        [self createBell];

    }

}

-(void)createBell

{

    SKSpriteNode *bell = [SKSpriteNode spriteNodeWithImageNamed:@”bell”];

    bell.name = @”bell”;

    bell.position = CGPointMake(CGRectGetMidX(self.frame), 100);

    [self addChild:bell];

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

    bell.physicsBody.linearDamping = 1.0;

    bell.physicsBody.angularDamping = 1.0;

    

    

    SKSpriteNode *top = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(10, 10)];

    top.position = CGPointMake(bell.position.x, bell.position.y + 80);

    [self addChild:top];

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

    top.physicsBody.dynamic = NO;

    

    SKPhysicsJointLimit *lope = [SKPhysicsJointLimit jointWithBodyA:bell.physicsBody bodyB:top.physicsBody anchorA:CGPointMake(bell.position.x, bell.position.y + 30) anchorB:top.position];

    lope.maxLength = 50;

    [self.physicsWorld addJoint:lope];

}

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

{

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

    [bell.physicsBody applyImpulse:CGVectorMake(10, 0) atPoint:CGPointMake(0.5, 0.1)];

    

    SKSpriteNode *boy = [SKSpriteNode spriteNodeWithImageNamed:@”bellboy”];

    boy.position = bell.position;

    [self addChild:boy];

    

    SKLabelNode *bonnou = [SKLabelNode node];

    bonnou.position = CGPointMake(-20, –30);

    bonnou.text = @”ぼ~ん;

    bonnou.fontColor = [SKColor whiteColor];

    bonnou.fontSize = 40;

    [boy addChild:bonnou];

    

    float x = arc4random() % 600;

    SKAction *move = [SKAction moveTo:CGPointMake(x, 400) duration:2.0];

    [boy runAction:move completion:^{

        [boy removeFromParent];

    }];

}

– (void)create2013

{

    SKLabelNode *year = [SKLabelNode node];

    year.text = @”2013″;

    year.fontColor = [[SKColor blackColor] colorWithAlphaComponent:0.3];

    year.fontSize = 180;

    year.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:year];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end