Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created February 19, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/b3f3e9f16cbb66065f942a0c24d9cf45 to your computer and use it in GitHub Desktop.
Save mzsima/b3f3e9f16cbb66065f942a0c24d9cf45 to your computer and use it in GitHub Desktop.
move tiles
//
// ViewController.m
// MoveTiles
//
// Created by MizushimaYusuke on 2017/02/19.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createTiles];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createTiles {
SKSpriteNode *base = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(300, 300)];
base.name = @"base";
base.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
[self.scene addChild:base];
[self create9:base];
}
- (void)create9:(SKNode*)node {
if (node.frame.size.width < 100) {
return;
}
for (int i=0; i<9; i++) {
float d = node.frame.size.width / 3.0;
float x = d * (i % 3) - d;
float y = d * (i / 3) - d;
SKShapeNode *tile = [SKShapeNode shapeNodeWithRect:CGRectMake(-d/2, -d/2, d, d) cornerRadius:5];
tile.fillColor = [UIColor colorWithHue:0.1 * i saturation:0.6 brightness:1 alpha:0.6];
tile.lineWidth = 0;
tile.position = CGPointMake(x, y);
[node addChild:tile];
[self create9:tile];
}
}
- (void)runPanel:(SKNode *)node {
if (node.children.count < 1) {
return;
}
for (int i=0; i<node.children.count; i++) {
SKNode *n = node.children[i];
int num = 0;
if (n.position.x < 0) {
if (n.position.y > 0) num = 7;
if (n.position.y == 0) num = 6;
if (n.position.y < 0) num = 3;
} else if (n.position.x == 0) {
if (n.position.y > 0) num = 8;
if (n.position.y == 0) num = 4;
if (n.position.y < 0) num = 0;
} else if (n.position.x > 0) {
if (n.position.y > 0) num = 5;
if (n.position.y == 0) num = 2;
if (n.position.y < 0) num = 1;
}
float d = node.frame.size.width / 3.0;
float x1 = d * (num % 3) - d;
float y1 = d * (num / 3) - d;
[n runAction:[SKAction moveTo:CGPointMake(x1, y1) duration:2.0]];
[self runPanel:n];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
SKNode *base = [self.scene childNodeWithName:@"base"];
[self runPanel:base];
}
@end
@mzsima
Copy link
Author

mzsima commented Feb 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment