Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // JellyFish
  4. //
  5. // Created by MizushimaYusuke on 2017/07/23.
  6. // Copyright © 2017 MizushimaYusuke. 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 createJellyFish];
  22. }
  23.  
  24. - (void)setupScene {
  25. SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
  26. SKScene * s = [SKScene sceneWithSize:sv.frame.size];
  27. [sv presentScene:s];
  28. [self.view addSubview:sv];
  29. self.scene = s;
  30. }
  31.  
  32. -(void)createJellyFish {
  33. UIColor *color = [UIColor colorWithHue:0.5 saturation:0.1 brightness:1 alpha:1];
  34. UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 60), NO, 0);
  35. [color set];
  36. CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 100, 100));
  37. CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
  38. CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(20, 45, 5, 5));
  39. CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(75, 45, 5, 5));
  40. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  41. UIGraphicsEndImageContext();
  42.  
  43. SKTexture *texture = [SKTexture textureWithImage:img];
  44. SKSpriteNode *jellyHead = [SKSpriteNode spriteNodeWithTexture:texture];
  45. jellyHead.name = @"head";
  46. jellyHead.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
  47. jellyHead.physicsBody = [SKPhysicsBody bodyWithTexture:texture size:texture.size];
  48. jellyHead.physicsBody.pinned = YES;
  49. [self.scene addChild:jellyHead];
  50.  
  51. float x = jellyHead.position.x - 40;
  52. for (int j=0; j<5; j++) {
  53. SKNode *previous = jellyHead;
  54. float y = jellyHead.position.y - 40;
  55. for (int i=0; i<4; i++) {
  56. SKSpriteNode *tentacle = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(4, 20)];
  57. tentacle.position = CGPointMake(x, y);
  58. [self.scene addChild:tentacle];
  59. tentacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:tentacle.size];
  60. SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:previous.physicsBody bodyB:tentacle.physicsBody anchor:CGPointMake(x, y + 10)];
  61. [self.scene.physicsWorld addJoint:pin];
  62.  
  63. y -= 20;
  64. previous = tentacle;
  65. }
  66. x += 20;
  67. }
  68. }
  69.  
  70. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  71. SKNode *head = [self.scene childNodeWithName:@"head"];
  72. [head.physicsBody applyTorque:0.3];
  73.  
  74. }
  75.  
  76. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement