Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // JellyFish
- //
- // Created by MizushimaYusuke on 2017/07/23.
- // 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 createJellyFish];
- }
- - (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)createJellyFish {
- UIColor *color = [UIColor colorWithHue:0.5 saturation:0.1 brightness:1 alpha:1];
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 60), NO, 0);
- [color set];
- CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 100, 100));
- CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
- CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(20, 45, 5, 5));
- CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(75, 45, 5, 5));
- UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- SKTexture *texture = [SKTexture textureWithImage:img];
- SKSpriteNode *jellyHead = [SKSpriteNode spriteNodeWithTexture:texture];
- jellyHead.name = @"head";
- jellyHead.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
- jellyHead.physicsBody = [SKPhysicsBody bodyWithTexture:texture size:texture.size];
- jellyHead.physicsBody.pinned = YES;
- [self.scene addChild:jellyHead];
- float x = jellyHead.position.x - 40;
- for (int j=0; j<5; j++) {
- SKNode *previous = jellyHead;
- float y = jellyHead.position.y - 40;
- for (int i=0; i<4; i++) {
- SKSpriteNode *tentacle = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(4, 20)];
- tentacle.position = CGPointMake(x, y);
- [self.scene addChild:tentacle];
- tentacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:tentacle.size];
- SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:previous.physicsBody bodyB:tentacle.physicsBody anchor:CGPointMake(x, y + 10)];
- [self.scene.physicsWorld addJoint:pin];
- y -= 20;
- previous = tentacle;
- }
- x += 20;
- }
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- SKNode *head = [self.scene childNodeWithName:@"head"];
- [head.physicsBody applyTorque:0.3];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement