四角と三角を重ねると反転するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController () <SKSceneDelegate>
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic, weak) SKNode *select;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createRectangle];
[self createTriangle];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:sv];
SKScene *scene = [[SKScene alloc] initWithSize:sv.frame.size];
scene.backgroundColor = [self color:0];
scene.delegate = self;
[sv presentScene:scene];
self.scene = scene;
}
– (void)createRectangle {
SKSpriteNode *rectNode = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(180, 180)];
rectNode.name = @”rectangle”;
rectNode.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
[self.scene addChild:rectNode];
}
– (void)createTriangle {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 45)];
[path addLineToPoint:CGPointMake(30, –45)];
[path addLineToPoint:CGPointMake(-30, –45)];
[path closePath];
SKShapeNode *triangle = [SKShapeNode shapeNodeWithPath:path.CGPath centered:YES];
triangle.name = @”triangle”;
triangle.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) – 100);
triangle.strokeColor = [UIColor clearColor];
triangle.fillColor = [self color:1];
[self.scene addChild:triangle];
SKNode *rect = [self.scene childNodeWithName:@”rectangle”];
UIBezierPath *pathZero = [UIBezierPath bezierPath];
[pathZero moveToPoint:CGPointZero];
for (int i=0; i<2; i++) {
SKShapeNode *triSub = [SKShapeNode shapeNodeWithPath:pathZero.CGPath];
triSub.name = @[@”triA”, @”triB”][i];
triSub.strokeColor = [UIColor clearColor];
triSub.fillColor = [self color:i];
triSub.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMinY(rect.frame));
[self.scene addChild:triSub];
}
}
– (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene
{
SKNode *rectangle = [self.scene childNodeWithName:@”rectangle”];
SKNode *triangle = [self.scene childNodeWithName:@”triangle”];
SKShapeNode *triA = (SKShapeNode *)[self.scene childNodeWithName:@”triA”];
SKShapeNode *triB = (SKShapeNode *)[self.scene childNodeWithName:@”triB”];
float dy = CGRectGetMaxY(triangle.frame) – CGRectGetMinY(rectangle.frame);
if (dy > 0) {
UIBezierPath *a = [UIBezierPath bezierPath];
[a moveToPoint:CGPointMake(0, dy)];
[a addLineToPoint:CGPointMake(dy / 3.0, 0)];
[a addLineToPoint:CGPointMake(-dy / 3.0, 0)];
[a closePath];
triA.path = a.CGPath;
[a applyTransform:CGAffineTransformMakeRotation(M_PI)];
triB.path = a.CGPath;
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.scene];
SKNode *triangle = [self.scene childNodeWithName:@”triangle”];
if ([triangle containsPoint:p]) {
self.select = triangle;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.scene];
[self.select runAction:[SKAction moveToY:p.y duration:0.2]];
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.select = nil;
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000)>>16)/255.0 green:((rgb & 0xFF00)>>8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]
– (UIColor *)color:(int)i {
switch (i) {
case 0: return ColorHex(0xEFECCA);
case 1: return ColorHex(0x046380);
}
return nil;
}
@end