マーカーで飛ぶタイミングを調節するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic, weak) SKNode *selectedmarker;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createOverlayConsole];
[self createGround];
[self createBlock];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.1 brightness:1 alpha:1];
sv.scene = [SCNScene scene];
[self.view addSubview:sv];
self.sceneView = sv;
sv.autoenablesDefaultLighting = YES;
}
– (void)createOverlayConsole {
self.sceneView.overlaySKScene = [SKScene sceneWithSize:self.view.bounds.size];
SKSpriteNode *console = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithWhite:0.1 alpha:0.5] size:CGSizeMake(CGRectGetMaxX(self.view.bounds), 160)];
console.name = @”console”;
console.position = CGPointMake(CGRectGetMidX(self.view.bounds), 80);
[self.sceneView.overlaySKScene addChild:console];
SKSpriteNode *start = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(80, 40)];
start.name = @”start”;
start.position = CGPointMake(0, –60);
[console addChild:start];
for (int i=0; i<5; i++) {
SKSpriteNode *line = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(CGRectGetMaxX(self.view.bounds), 1)];
line.position = CGPointMake(0, 25 * i – 40);
[console addChild:line];
}
for (int i=0; i<3; i++) {
SKShapeNode *marker = [SKShapeNode shapeNodeWithCircleOfRadius:15];
marker.name = @”marker”;
marker.fillColor = [UIColor colorWithHue:0.2 * i saturation:0.7 brightness:1 alpha:0.8];
marker.lineWidth = 3;
marker.position = CGPointMake(CGRectGetMaxX(self.view.bounds) / 4.0 * (i – 1), 0);
[console addChild:marker];
}
}
– (void)createGround {
SCNBox *ground = [SCNBox boxWithWidth:10 height:0.2 length:3 chamferRadius:0];
ground.firstMaterial.diffuse.contents = [UIColor brownColor];
SCNNode *groundNode = [SCNNode nodeWithGeometry:ground];
[self.sceneView.scene.rootNode addChildNode:groundNode];
}
– (void)createBlock {
for (int i=0; i<3; i++) {
SCNBox *box = [SCNBox boxWithWidth:2 height:2 length:2 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.2 * i saturation:0.7 brightness:1 alpha:1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = [NSString stringWithFormat:@”box%d”, i];
boxNode.position = SCNVector3Make(3 * (i – 1), 1.1, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 2, 10);
camera.rotation = SCNVector4Make(1, 0, 0, –0.2);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.sceneView.playing = YES;
SKNode *console = [self.sceneView.overlaySKScene childNodeWithName:@”console”];
CGPoint p = [[touches anyObject] locationInNode:console];
SKNode *hit = [console nodeAtPoint:p];
if ([hit.name isEqual:@”start”]) {
[hit runAction:[SKAction fadeAlphaTo:0.4 duration:0.3] completion:^{
[hit runAction:[SKAction fadeInWithDuration:0.2]];
}];
__block int count = 0;
[console enumerateChildNodesWithName:@”marker” usingBlock:^(SKNode *node, BOOL *stop) {
float t = (node.position.x + CGRectGetMidX(self.view.bounds)) / 200;
float h = (node.position.y + 80) / 50;
SCNNode *box = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”box%d”, count] recursively:NO];
SCNVector3 o = box.position;
[box runAction:[SCNAction sequence:@[
[SCNAction waitForDuration:t],
[SCNAction moveByX:0 y:h z:0 duration:0.5]]]
completionHandler:^{
[box runAction:[SCNAction moveTo:o duration:0.5]];
}];
count++;
}];
}
if ([hit.name isEqual:@”marker”]) {
self.selectedmarker = hit;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.selectedmarker) {
SKNode *console = [self.sceneView.overlaySKScene childNodeWithName:@”console”];
CGPoint p = [[touches anyObject] locationInNode:console];
self.selectedmarker.position = p;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.selectedmarker = nil;
}
@end