Created
August 27, 2016 13:54
-
-
Save mzsima/f4e2fbbcf6e175376eaee41988f36a30 to your computer and use it in GitHub Desktop.
hole slope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// HoleSlope | |
// | |
// Created by MizushimaYusuke on 8/27/16. | |
// Copyright © 2016 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 createHole]; | |
} | |
- (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)createHole { | |
CGSize size = CGSizeMake(CGRectGetMaxX(self.view.bounds), CGRectGetMaxY(self.view.bounds) * 0.7); | |
UIGraphicsBeginImageContextWithOptions(size, false, true); | |
[[UIColor brownColor] set]; | |
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height)); | |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); | |
CGPoint points[] = { | |
{50, 0}, {100, 100}, | |
{100, 100}, {300, 150}, | |
{300, 150}, {200, 350}, | |
{200, 350}, {300, 400} | |
}; | |
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 30); | |
for (int i=0; i<8; i += 2) { | |
CGPoint line[2] = {points[i], points[i+1]}; | |
CGContextStrokeLineSegments(UIGraphicsGetCurrentContext(), line, 2); | |
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(line[0].x - 20 , line[0].y - 20, 40, 40)); | |
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(line[1].x - 20 , line[1].y - 20, 40, 40)); | |
} | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
SKTexture *texture = [SKTexture textureWithImage:img]; | |
SKSpriteNode *hole = [SKSpriteNode spriteNodeWithTexture:texture]; | |
hole.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) * 0.35); | |
[self.scene addChild:hole]; | |
hole.physicsBody = [SKPhysicsBody bodyWithTexture:texture size:texture.size]; | |
hole.physicsBody.dynamic = false; | |
} | |
- (void)createBall { | |
UIGraphicsBeginImageContextWithOptions(CGSizeMake(50, 50), false, 0); | |
[[UIColor darkGrayColor] set]; | |
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(20, 5, 10, 12)); | |
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(20, 15, 10, 10)); | |
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(18, 25, 14, 22)); | |
CGPoint points[] = { | |
{25, 10} , {20, 0}, | |
{25, 10} , {30, 0}, | |
{25, 25} , {10, 10}, | |
{25, 25} , {40, 10}, | |
{25, 25} , {10, 25}, | |
{25, 25} , {40, 25}, | |
{25, 25} , {10, 40}, | |
{25, 25} , {40, 40}, | |
}; | |
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2); | |
for (int i=0; i<14; i += 2) { | |
CGPoint line[2] = {points[i], points[i+1]}; | |
CGContextStrokeLineSegments(UIGraphicsGetCurrentContext(), line, 2); | |
} | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
SKTexture *texture = [SKTexture textureWithImage:img]; | |
SKSpriteNode *ant = [SKSpriteNode spriteNodeWithTexture:texture]; | |
ant.position = CGPointMake(50, CGRectGetMaxY(self.view.bounds) - 100); | |
[self.scene addChild:ant]; | |
ant.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:13]; | |
ant.physicsBody.angularDamping = 1.0; | |
ant.physicsBody.friction = 0.1; | |
} | |
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { | |
[self createBall]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=5449