iPhoneカードでプログラミング

カードを使って、黄色い四角の動きをプログラミングする。そんなかんじのプログラム少年育成用iPhoneアプリのサンプルコードを描いてみます。

今回使った画像


サンプルを動画で確認

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface CardScene : SKScene

@property BOOL contentCreated;

@property (weak, nonatomic) SKNode *moveCard;

@property (strong, nonatomic) NSMutableArray *boxArr;

@end

@implementation CardScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

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

        SKSpriteNode *card = [SKSpriteNode spriteNodeWithImageNamed:@”arrow”];

        card.name = @”card”;

        card.size = CGSizeMake(50, 50);

        card.position = CGPointMake(75 + 55 * i, 80);

        card.zRotation = i * (M_PI / 2.0);

        [self addChild:card];

    }

    

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    start.frame = CGRectMake(CGRectGetMidX(self.frame) – 50, CGRectGetMaxY(self.frame) – 40, 100, 30);

    start.backgroundColor = [UIColor whiteColor];

    [start setTitle:@”START” forState:UIControlStateNormal];

    [self.view addSubview:start];

    [start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];

    self.boxArr = [[NSMutableArray alloc] init];

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

        SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(50, 50)];

        box.name = [NSString stringWithFormat:@”box%d”, i];

        float x = (i % 5) * 60 + 40;

        float y = – (i / 5) * 60 + 200;

        box.position = CGPointMake(x, y);

        [self addChild:box];

        [self.boxArr addObject:box];

        

        SKLabelNode *num = [SKLabelNode node];

        num.text = [@(i + 1) stringValue];

        num.fontColor = [UIColor lightGrayColor];

        num.position = CGPointMake(0, –12);

        [box addChild:num];

    }

    

    SKSpriteNode *mark = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(30, 30)];

    mark.name = @”mark”;

    mark.position = CGPointMake(160, CGRectGetMaxY(self.frame)-160);

    [self addChild:mark];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

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

        if (CGRectContainsPoint(node.frame, p)) {

            SKNode *card = [node copy];

            card.name = @”copyCard”;

            [self addChild:card];

            self.moveCard = card;

        }

    }];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    if (self.moveCard) {

        self.moveCard.position = p;

    }

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    

    BOOL outsideBox = YES;

    for (int i=0; i<[self.boxArr count]; i++) {

        SKNode *node = [self.boxArr objectAtIndex:i];

        if (CGRectContainsPoint(node.frame, p)) {

            self.moveCard.position = node.position;

            

            NSString *name = [NSString stringWithFormat:@”card%d”, i];

            

            // delete old

            [[self childNodeWithName:name] removeFromParent];

            self.moveCard.name = name;

            

            outsideBox = NO;

        }

    }

    

    if (outsideBox) {

        SKNode *dust = self.moveCard;

        SKAction *fade = [SKAction fadeAlphaTo:0 duration:0.5];

        [dust runAction:fade completion:^{

            [dust removeFromParent];

        }];

    }

    

    self.moveCard = nil;

}

– (void)start

{

    NSMutableArray *actions = [[NSMutableArray alloc] init];

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

        NSString *cardName = [NSString stringWithFormat:@”card%d”, i];

        SKSpriteNode *card = (SKSpriteNode*)[self childNodeWithName:cardName];

        SKAction *action;

        if (card) {

            float dx = 30 * cos(card.zRotation);

            float dy = 30 * sin(card.zRotation);

            action = [SKAction moveByX:dx y:dy duration:0.5];

            [actions addObject:action];

        } else {

            [actions addObject:[SKAction waitForDuration:0.5]];

        }

    }

    

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

    [mark runAction:[SKAction sequence:actions] completion:^{

        [self reset];

    }];

}

– (void)reset

{

    [self removeAllChildren];

    [self createSceneContents];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end