iPhoneバグライト

バグ(アリ)をライトで照らしだすようなiPhoneアプリのサンプルコードを描いてみます。



使った画像

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface SearchScene : SKScene

@end

@implementation SearchScene

– (void)didMoveToView:(SKView *)view

{

    [self createBox];

    [self createLight];

    [self createAnts];

}

– (void)createBox

{

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    self.physicsBody.dynamic = NO;

    self.physicsWorld.gravity = CGVectorMake(0, 0);

}

– (void)createLight

{

    SKNode *light = [SKNode node];

    light.name = @”light”;

    light.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:light];

    

    SKSpriteNode *lamp = [SKSpriteNode spriteNodeWithImageNamed:@”light”];

    lamp.zPosition = 2;

    [light addChild:lamp];

    

    float r = 120;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointZero];

    [path addLineToPoint:CGPointMake(r * cos(-M_PI_4), r * sin(-M_PI_4))];

    [path addArcWithCenter:CGPointZero radius:r startAngle:-M_PI_4 endAngle:M_PI_4 clockwise:YES];

    [path addLineToPoint:CGPointZero];

    SKShapeNode *lightArea = [SKShapeNode node];

    lightArea.name = @”lightArea”;

    lightArea.path = path.CGPath;

    lightArea.fillColor = [[SKColor yellowColor] colorWithAlphaComponent:0.4];

    lightArea.zRotation = M_PI_2;

    [light addChild:lightArea];

    

    [lightArea runAction:[SKAction repeatActionForever:[SKAction customActionWithDuration:2.0 actionBlock:^(SKNode *node, CGFloat elapsedTime) {

        float a = (elapsedTime < 1.0) ? 0.8 * elapsedTime : 0.8 * (2.0 – elapsedTime);

        lightArea.fillColor = [[SKColor yellowColor] colorWithAlphaComponent:a];

    }]]];

    

    [light runAction:[SKAction repeatActionForever:[SKAction rotateByAngle:2*M_PI duration:5.0]]];

}

– (void)createAnts

{

    for (int i=0; i<10; i++) {

        SKSpriteNode *ant = [SKSpriteNode spriteNodeWithImageNamed:@”ant”];

        ant.name = @”ant”;

        ant.position = CGPointMake(arc4random() % 300 + 10, arc4random() % 300 + 100);

        ant.alpha = 0.1;

        [self addChild:ant];

        

        ant.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ant.size];

        ant.physicsBody.restitution = 1.0;

        ant.physicsBody.linearDamping = 0;

    }

}

– (void)didSimulatePhysics

{

    SKNode *light = [self childNodeWithName:@”light”];

    SKShapeNode *lightArea = (SKShapeNode *)[light childNodeWithName:@”lightArea”];

    [self enumerateChildNodesWithName:@”ant” usingBlock:^(SKNode *node, BOOL *stop) {

        CGPoint p = [node convertPoint:CGPointZero toNode:lightArea];

        if (CGPathContainsPoint(lightArea.path, &CGAffineTransformIdentity, p, NO))

        {

            node.alpha = 1.0;

        } else {

            node.alpha = 0.1;

        }

    }];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self enumerateChildNodesWithName:@”ant” usingBlock:^(SKNode *node, BOOL *stop) {

        int dx = (arc4random() % 50) – 25;

        int dy = (arc4random() % 50) – 25;

        [node.physicsBody applyForce:CGVectorMake(dx, dy)];

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[SearchScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end