Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // StagBeetle
- //
- // Created by Mizusima Yusuke on 2017/08/17.
- // Copyright © 2017 Mizusima Yusuke. 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 createStag];
- }
- - (void)setupScene {
- SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
- SKScene *s = [SKScene sceneWithSize:sv.frame.size];
- s.backgroundColor = [UIColor brownColor];
- [sv presentScene:s];
- [self.view addSubview:sv];
- self.scene = s;
- }
- - (void)createStag {
- UIColor *color = [UIColor colorWithHue:0.5 saturation:0.9 brightness:0.1 alpha:1] ;
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(80, 80), NO, 0);
- [color set];
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGContextFillRect(ctx, CGRectMake(15, 0, 50, 28));
- CGContextFillRect(ctx, CGRectMake(15, 30, 50, 50));
- CGContextFillEllipseInRect(ctx, CGRectMake(10, 10, 10, 10));
- CGContextFillEllipseInRect(ctx, CGRectMake(60, 10, 10, 10));
- CGContextFillRect(ctx, CGRectMake(0, 35, 80, 5));
- CGContextFillRect(ctx, CGRectMake(0, 45, 80, 5));
- CGContextFillRect(ctx, CGRectMake(0, 60, 80, 5));
- UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- SKTexture *texture = [SKTexture textureWithImage:img];
- SKSpriteNode *stag = [SKSpriteNode spriteNodeWithTexture:texture];
- stag.position = CGPointMake(100, 50);
- stag.zRotation = -M_PI * 0.3;
- stag.name = @"stag";
- [self.scene addChild:stag];
- for (int i=0; i<2; i++) {
- SKSpriteNode *jaw = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(10, 50)];
- jaw.name = [NSString stringWithFormat:@"jaw%d", i];
- jaw.anchorPoint = CGPointMake(0.5, 0);
- jaw.position = CGPointMake(-20 + i * 40, 40);
- jaw.zRotation = i == 0 ? M_PI * 0.05 : -M_PI * 0.05;
- [stag addChild:jaw];
- }
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- SKNode *stag = [self.scene childNodeWithName:@"stag"];
- float w = stag.zRotation + M_PI * 0.5;
- SKAction *move = [SKAction moveBy:CGVectorMake(80 * cos(w), 80 * sin(w)) duration:1.0];
- [stag runAction:move];
- for (SKNode *child in stag.children) {
- if ([child.name hasPrefix:@"jaw"]) {
- int n = [[child.name componentsSeparatedByString:@"jaw"][1] intValue];
- SKAction *wait = [SKAction waitForDuration:1.0];
- SKAction *close = [SKAction rotateByAngle:(n == 1 ? M_PI * 0.15 : -M_PI * 0.15) duration:1.0];
- SKAction *open = [close reversedAction];
- [child runAction:[SKAction sequence:@[wait, close, open]]];
- }
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement