Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // hourglass
- //
- // Created by MizushimaYusuke on 6/28/16.
- // Copyright © 2016 MizushimaYusuke. All rights reserved.
- //
- #import "ViewController.h"
- @import SpriteKit;
- @interface ViewController () <SKSceneDelegate>
- @property (nonatomic ,weak) SKScene *scene;
- @property (nonatomic) BOOL startHour;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setupScene];
- [self createHourglass];
- [self createStart];
- }
- - (void)setupScene {
- SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
- SKScene *s = [SKScene sceneWithSize:sv.frame.size];
- s.delegate = self;
- [sv presentScene:s];
- [self.view addSubview:sv];
- self.scene = s;
- }
- - (void)createHourglass {
- CGPoint o = CGPointMake(50, 100);
- UIBezierPath *path = [UIBezierPath bezierPath];
- [path moveToPoint:CGPointMake(o.x, o.y + 100)];
- [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)];
- [path addCurveToPoint:CGPointMake(o.x + 5, o.y) controlPoint1:CGPointMake(o.x + 50, o.y + 40) controlPoint2:CGPointMake(o.x, o.y + 20)];
- [path addCurveToPoint:CGPointMake(o.x + 50, o.y - 70) controlPoint1:CGPointMake(o.x, o.y - 20) controlPoint2:CGPointMake(o.x + 50, o.y - 40)];
- [path addCurveToPoint:CGPointMake(o.x, o.y - 100) controlPoint1:CGPointMake(o.x + 50, o.y - 100) controlPoint2:CGPointMake(o.x + 20, o.y - 100)];
- [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)];
- [path addCurveToPoint:CGPointMake(o.x - 5, o.y) controlPoint1:CGPointMake(o.x - 50, o.y - 40) controlPoint2:CGPointMake(o.x, o.y - 20)];
- [path addCurveToPoint:CGPointMake(o.x - 50, o.y + 70) controlPoint1:CGPointMake(o.x, o.y + 20) controlPoint2:CGPointMake(o.x - 50, o.y + 40)];
- [path addCurveToPoint:CGPointMake(o.x, o.y + 100) controlPoint1:CGPointMake(o.x - 50, o.y + 100) controlPoint2:CGPointMake(o.x - 20, o.y + 100)];
- path.lineWidth = 2;
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(102, 202), false, 0);
- CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 1, 1);
- [[UIColor whiteColor] set];
- [path stroke];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- SKTexture *texture = [SKTexture textureWithImage:image];
- SKSpriteNode *n = [SKSpriteNode spriteNodeWithTexture:texture];
- n.name = @"glass";
- [self.scene addChild:n];
- n.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
- CGAffineTransform bt = CGAffineTransformMakeTranslation(-50, -100);
- UIBezierPath *bodyPath = [UIBezierPath bezierPathWithCGPath:CGPathCreateCopyByTransformingPath(path.CGPath, &bt)];
- n.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:bodyPath.CGPath];
- n.physicsBody.pinned = YES;
- }
- - (void)createStart {
- SKLabelNode *start = [SKLabelNode labelNodeWithText:@"▲"];
- start.name = @"start";
- start.fontSize = 50;
- start.zRotation = -M_PI * 0.5;
- start.position = CGPointMake(CGRectGetMaxX(self.view.bounds) - 100, CGRectGetMidY(self.view.bounds) - 80);
- [self.scene addChild:start];
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- CGPoint p = [[touches anyObject] locationInNode:self.scene];
- SKNode *hit = [self.scene nodeAtPoint:p];
- if ([hit.name isEqual:@"start"]) {
- self.startHour = YES;
- return;
- }
- for (int i=0; i<10; i++) {
- SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(5, 5)];
- n.position = p;
- [self.scene addChild:n];
- n.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:2.5];
- n.physicsBody.friction = 0;
- }
- }
- - (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene {
- SKNode *glass = [self.scene childNodeWithName:@"glass"];
- if (self.startHour) {
- glass.physicsBody.angularVelocity = 0.5;
- } else {
- glass.physicsBody.angularVelocity = 0;
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement