地面にあるものをUFOが吸い込むiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic) BOOL vacuum;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createObjects];
[self createUFO];
}
– (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
[sv presentScene:s];
s.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.view.bounds];
[self.view addSubview:sv];
self.scene = s;
SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:[self imageWithColor:[UIColor grayColor]]] size:self.view.bounds.size];
background.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
// background.lightingBitMask = 0x1;
[self.scene addChild:background];
}
– (void)createObjects {
float dx = CGRectGetMaxX(self.view.bounds) / 5.0;
float dy = CGRectGetMaxY(self.view.bounds) / 10.0;
for (int i=0; i<40; i++) {
float x = (i % 5) * dx + dx/2.0;
float y = (i / 5) * dy;
SKSpriteNode *obj = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:0.1 * (i%10) saturation:0.5 brightness:1 alpha:1] size:CGSizeMake(30, 30)];
obj.position = CGPointMake(x, y);
obj.zRotation = M_PI/4.0;
obj.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obj.size];
[self.scene addChild:obj];
}
}
– (void)createUFO {
SKShapeNode *ufo = [SKShapeNode shapeNodeWithEllipseOfSize:CGSizeMake(70, 30)];
ufo.name = @”ufo”;
ufo.strokeColor = [UIColor clearColor];
ufo.fillColor = [UIColor lightGrayColor];
ufo.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) – 80);
ufo.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
ufo.physicsBody.dynamic = NO;
[self.scene addChild:ufo];
for (int i=0; i<5; i++) {
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(5, 5)];
n.position = CGPointMake(-20 + 10 * i, 0);
[ufo addChild:n];
}
// SKLightNode *l = [SKLightNode node];
// l.lightColor = [UIColor whiteColor];
// [ufo addChild:l];
CGPoint points[] = {CGPointMake(-30, –20), CGPointMake(30, –20), CGPointMake(100, –380), CGPointMake(-100, –380)};
SKShapeNode *light = [SKShapeNode shapeNodeWithPoints:points count:4];
light.name = @”light”;
light.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.2];
light.strokeColor = [UIColor clearColor];
light.alpha = 0;
[ufo addChild:light];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *ufo = [self.scene childNodeWithName:@”ufo”];
SKNode *light = [ufo childNodeWithName:@”light”];
if (self.vacuum) {
light.alpha = 0.0;
[self.scene enumerateChildNodesWithName:@”vacuume field” usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
self.vacuum = NO;
} else {
light.alpha = 1.0;
for (int i=0; i<4; i++) {
SKFieldNode *fNode = [SKFieldNode radialGravityField];
fNode.name = @”vacuume field”;
fNode.position = CGPointMake(ufo.position.x, ufo.position.y – 100 * i – 50);
fNode.strength = 8.0 – i;
fNode.falloff = 1;
[self.scene addChild:fNode];
}
self.vacuum = YES;
}
}
– (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 40, 40);
UIGraphicsBeginImageContext(rect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, color.CGColor);
CGContextFillRect(ctx, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end