Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // StagBeetle
  4. //
  5. // Created by Mizusima Yusuke on 2017/08/17.
  6. // Copyright © 2017 Mizusima Yusuke. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. @import SpriteKit;
  11.  
  12. @interface ViewController ()
  13. @property (nonatomic, weak) SKScene *scene;
  14. @end
  15.  
  16. @implementation ViewController
  17.  
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self setupScene];
  21. [self createStag];
  22. }
  23.  
  24. - (void)setupScene {
  25. SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
  26. SKScene *s = [SKScene sceneWithSize:sv.frame.size];
  27. s.backgroundColor = [UIColor brownColor];
  28. [sv presentScene:s];
  29. [self.view addSubview:sv];
  30. self.scene = s;
  31. }
  32.  
  33. - (void)createStag {
  34. UIColor *color = [UIColor colorWithHue:0.5 saturation:0.9 brightness:0.1 alpha:1] ;
  35. UIGraphicsBeginImageContextWithOptions(CGSizeMake(80, 80), NO, 0);
  36. [color set];
  37. CGContextRef ctx = UIGraphicsGetCurrentContext();
  38. CGContextFillRect(ctx, CGRectMake(15, 0, 50, 28));
  39. CGContextFillRect(ctx, CGRectMake(15, 30, 50, 50));
  40. CGContextFillEllipseInRect(ctx, CGRectMake(10, 10, 10, 10));
  41. CGContextFillEllipseInRect(ctx, CGRectMake(60, 10, 10, 10));
  42.  
  43. CGContextFillRect(ctx, CGRectMake(0, 35, 80, 5));
  44. CGContextFillRect(ctx, CGRectMake(0, 45, 80, 5));
  45. CGContextFillRect(ctx, CGRectMake(0, 60, 80, 5));
  46.  
  47. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  48. UIGraphicsEndImageContext();
  49.  
  50. SKTexture *texture = [SKTexture textureWithImage:img];
  51. SKSpriteNode *stag = [SKSpriteNode spriteNodeWithTexture:texture];
  52. stag.position = CGPointMake(100, 50);
  53. stag.zRotation = -M_PI * 0.3;
  54. stag.name = @"stag";
  55. [self.scene addChild:stag];
  56.  
  57. for (int i=0; i<2; i++) {
  58. SKSpriteNode *jaw = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(10, 50)];
  59. jaw.name = [NSString stringWithFormat:@"jaw%d", i];
  60. jaw.anchorPoint = CGPointMake(0.5, 0);
  61. jaw.position = CGPointMake(-20 + i * 40, 40);
  62. jaw.zRotation = i == 0 ? M_PI * 0.05 : -M_PI * 0.05;
  63. [stag addChild:jaw];
  64. }
  65. }
  66.  
  67. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  68. SKNode *stag = [self.scene childNodeWithName:@"stag"];
  69. float w = stag.zRotation + M_PI * 0.5;
  70. SKAction *move = [SKAction moveBy:CGVectorMake(80 * cos(w), 80 * sin(w)) duration:1.0];
  71. [stag runAction:move];
  72.  
  73. for (SKNode *child in stag.children) {
  74. if ([child.name hasPrefix:@"jaw"]) {
  75. int n = [[child.name componentsSeparatedByString:@"jaw"][1] intValue];
  76. SKAction *wait = [SKAction waitForDuration:1.0];
  77. SKAction *close = [SKAction rotateByAngle:(n == 1 ? M_PI * 0.15 : -M_PI * 0.15) duration:1.0];
  78. SKAction *open = [close reversedAction];
  79. [child runAction:[SKAction sequence:@[wait, close, open]]];
  80. }
  81. }
  82. }
  83.  
  84. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement