
光に見立てた黄色のつぶつぶをスリットに向けてとばすiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface SlitScene : SKScene
@property (nonatomic) BOOL lighting;
@property (nonatomic, weak) SKShapeNode *canvas;
@end
@implementation SlitScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor colorWithWhite:0.05 alpha:1];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createSlit];
[self createLight];
[self createCanvas];
}
– (void)createSlit
{
SKNode *slit = [SKNode node];
slit.position = CGPointMake(160, 250);
[self addChild:slit];
NSMutableArray *bodies = [NSMutableArray array];
for (int i=0; i<3; i++) {
SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(100, 20)];
block.position = CGPointMake(110 * i – 110, 0);
[slit addChild:block];
[bodies addObject:[SKPhysicsBody bodyWithRectangleOfSize:block.size center:block.position]];
}
slit.physicsBody = [SKPhysicsBody bodyWithBodies:bodies];
slit.physicsBody.dynamic = NO;
}
– (void)createLight
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *light = [SKShapeNode node];
light.fillColor = [SKColor yellowColor];
light.strokeColor = [SKColor blackColor];
light.path = path.CGPath;
light.position = CGPointMake(160, 50);
[self addChild:light];
}
– (void)createCanvas
{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 1, 1)];
SKShapeNode *canvas = [SKShapeNode node];
canvas.strokeColor = [[SKColor yellowColor] colorWithAlphaComponent:0.4];
canvas.fillColor = [SKColor clearColor];
canvas.path = path.CGPath;
[self addChild:canvas];
self.canvas = canvas;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.lighting = YES;
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.lighting = NO;
}
– (void)update:(NSTimeInterval)currentTime
{
if (self.lighting) {
SKSpriteNode *light = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(4, 4)];
light.name = @”light”;
light.position = CGPointMake(160, 50);
[self addChild:light];
light.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];
light.physicsBody.restitution = 1.0;
[light.physicsBody applyImpulse:CGVectorMake((arc4random() % 100) * 0.01 – 0.5, (arc4random() % 100) * 0.01 – 0.5)];
[light performSelector:@selector(removeFromParent) withObject:nil afterDelay:2.0];
}
UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:self.canvas.path];
[self enumerateChildNodesWithName:@”light” usingBlock:^(SKNode *node, BOOL *stop) {
if(node.position.y > 260) {
node.physicsBody.collisionBitMask = 0x0;
[path appendPath:[UIBezierPath bezierPathWithRect:node.frame]];
}
}];
self.canvas.path = path.CGPath;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[SlitScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end