Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // hourglass
  4. //
  5. // Created by MizushimaYusuke on 6/28/16.
  6. // Copyright © 2016 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. @import SpriteKit;
  11.  
  12. @interface ViewController () <SKSceneDelegate>
  13. @property (nonatomic ,weak) SKScene *scene;
  14. @property (nonatomic) BOOL startHour;
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self setupScene];
  22. [self createHourglass];
  23. [self createStart];
  24. }
  25.  
  26. - (void)setupScene {
  27. SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
  28. SKScene *s = [SKScene sceneWithSize:sv.frame.size];
  29. s.delegate = self;
  30. [sv presentScene:s];
  31. [self.view addSubview:sv];
  32. self.scene = s;
  33. }
  34.  
  35. - (void)createHourglass {
  36. CGPoint o = CGPointMake(50, 100);
  37. UIBezierPath *path = [UIBezierPath bezierPath];
  38. [path moveToPoint:CGPointMake(o.x, o.y + 100)];
  39. [path addCurveToPoint:CGPointMake(o.x + 50, o.y + 70) controlPoint1:CGPointMake(o.x + 20, o.y + 100) controlPoint2:CGPointMake(o.x + 50, o.y + 100)];
  40. [path addCurveToPoint:CGPointMake(o.x + 5, o.y) controlPoint1:CGPointMake(o.x + 50, o.y + 40) controlPoint2:CGPointMake(o.x, o.y + 20)];
  41. [path addCurveToPoint:CGPointMake(o.x + 50, o.y - 70) controlPoint1:CGPointMake(o.x, o.y - 20) controlPoint2:CGPointMake(o.x + 50, o.y - 40)];
  42. [path addCurveToPoint:CGPointMake(o.x, o.y - 100) controlPoint1:CGPointMake(o.x + 50, o.y - 100) controlPoint2:CGPointMake(o.x + 20, o.y - 100)];
  43. [path addCurveToPoint:CGPointMake(o.x - 50, o.y - 70) controlPoint1:CGPointMake(o.x - 20, o.y - 100) controlPoint2:CGPointMake(o.x - 50, o.y - 100)];
  44. [path addCurveToPoint:CGPointMake(o.x - 5, o.y) controlPoint1:CGPointMake(o.x - 50, o.y - 40) controlPoint2:CGPointMake(o.x, o.y - 20)];
  45. [path addCurveToPoint:CGPointMake(o.x - 50, o.y + 70) controlPoint1:CGPointMake(o.x, o.y + 20) controlPoint2:CGPointMake(o.x - 50, o.y + 40)];
  46. [path addCurveToPoint:CGPointMake(o.x, o.y + 100) controlPoint1:CGPointMake(o.x - 50, o.y + 100) controlPoint2:CGPointMake(o.x - 20, o.y + 100)];
  47. path.lineWidth = 2;
  48.  
  49.  
  50. UIGraphicsBeginImageContextWithOptions(CGSizeMake(102, 202), false, 0);
  51. CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 1, 1);
  52. [[UIColor whiteColor] set];
  53. [path stroke];
  54.  
  55. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  56. UIGraphicsEndImageContext();
  57.  
  58. SKTexture *texture = [SKTexture textureWithImage:image];
  59. SKSpriteNode *n = [SKSpriteNode spriteNodeWithTexture:texture];
  60. n.name = @"glass";
  61. [self.scene addChild:n];
  62.  
  63. n.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
  64.  
  65. CGAffineTransform bt = CGAffineTransformMakeTranslation(-50, -100);
  66. UIBezierPath *bodyPath = [UIBezierPath bezierPathWithCGPath:CGPathCreateCopyByTransformingPath(path.CGPath, &bt)];
  67.  
  68. n.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:bodyPath.CGPath];
  69. n.physicsBody.pinned = YES;
  70. }
  71.  
  72. - (void)createStart {
  73. SKLabelNode *start = [SKLabelNode labelNodeWithText:@"▲"];
  74. start.name = @"start";
  75. start.fontSize = 50;
  76. start.zRotation = -M_PI * 0.5;
  77. start.position = CGPointMake(CGRectGetMaxX(self.view.bounds) - 100, CGRectGetMidY(self.view.bounds) - 80);
  78. [self.scene addChild:start];
  79.  
  80. }
  81.  
  82. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  83. CGPoint p = [[touches anyObject] locationInNode:self.scene];
  84.  
  85. SKNode *hit = [self.scene nodeAtPoint:p];
  86. if ([hit.name isEqual:@"start"]) {
  87. self.startHour = YES;
  88. return;
  89. }
  90.  
  91. for (int i=0; i<10; i++) {
  92. SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(5, 5)];
  93. n.position = p;
  94. [self.scene addChild:n];
  95. n.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:2.5];
  96. n.physicsBody.friction = 0;
  97. }
  98. }
  99.  
  100. - (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene {
  101. SKNode *glass = [self.scene childNodeWithName:@"glass"];
  102. if (self.startHour) {
  103. glass.physicsBody.angularVelocity = 0.5;
  104. } else {
  105. glass.physicsBody.angularVelocity = 0;
  106. }
  107. }
  108.  
  109. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement