
シマシマがゆらゆらするiPhoneアプリのサンプルコードを描いてみます
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface StripeScene : SKScene
@end
@implementation StripeScene
– (void)didMoveToView:(SKView *)view
{
self.physicsWorld.speed = 0.5;
self.backgroundColor = [UIColor whiteColor];
for (int i=0; i<4; i++) {
SKColor *color = [SKColor colorWithHue:i * 0.15 saturation:0.7 brightness:1.0 alpha:0.5];
[self createShapes:color idx:i];
}
}
– (void)createShapes:(SKColor *)color idx:(int)idx
{
SKSpriteNode *top = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(10, 10)];
top.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 50);
[self addChild:top];
top.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:top.size];
top.physicsBody.dynamic = NO;
SKNode *pre = top;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];
SKShapeNode *mask = [SKShapeNode node];
mask.path = path.CGPath;
mask.strokeColor = [SKColor whiteColor];
mask.lineWidth = 40;
for (int i=0; i<3; i++) {
SKCropNode *n = [SKCropNode node];
n.name = [NSString stringWithFormat:@”shape%d”, (3 * idx) + i +1];
n.position = CGPointMake(160, top.position.y – (i+1) * 100);
for (int j=0; j<5; j++) {
SKSpriteNode *a = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(150, 10)];
a.position = CGPointMake(0, idx % 2 ? (20 * j – 40) : (20 * j – 30));
[n addChild:a];
}
[n setMaskNode:mask];
[self addChild:n];
n.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
n.physicsBody.categoryBitMask = 0x1;
n.physicsBody.collisionBitMask = 0x2;
SKPhysicsJointLimit *l = [SKPhysicsJointLimit jointWithBodyA:pre.physicsBody bodyB:n.physicsBody anchorA:CGPointMake(pre.position.x, pre.position.y) anchorB:CGPointMake(n.position.x, n.position.y + 30)];
[self.physicsWorld addJoint:l];
pre = n;
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (int i=0; i<5; i++) {
SKNode *n = [self childNodeWithName:[NSString stringWithFormat:@”shape%d”, i * 3]];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[n.physicsBody applyImpulse:CGVectorMake(15, 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 = [[StripeScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end