Created
January 11, 2017 11:08
-
-
Save mime29/e9649a99aa7fd1a669cae3db67ccd8e3 to your computer and use it in GitHub Desktop.
it's a ball that rotates
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import XCPlayground | |
import SpriteKit | |
class FourColorCircle : SKNode { | |
override init() { | |
super.init() | |
self.createCircle() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
func createCircle () { | |
let center = CGPoint(x:self.frame.midX, y:self.frame.midY) | |
// node1 | |
let node1bezierPath = UIBezierPath() | |
node1bezierPath.addArc(withCenter: center, radius: 100, startAngle: 0.78, endAngle: 2.35, clockwise: true) | |
node1bezierPath.addLine(to: center) | |
let node1 = SKShapeNode(path: node1bezierPath.cgPath) | |
node1.strokeColor = SKColor.red | |
node1.fillColor = SKColor.red | |
self.addChild(node1) | |
// node2 | |
let node2bezierPath = UIBezierPath() | |
node2bezierPath.addArc(withCenter: center, radius: 100, startAngle: 2.35, endAngle: 3.92, clockwise: true) | |
node2bezierPath.addLine(to: center) | |
let node2 = SKShapeNode(path: node2bezierPath.cgPath) | |
node2.strokeColor = SKColor.blue | |
node2.fillColor = SKColor.blue | |
self.addChild(node2) | |
// node3 | |
let node3bezierPath = UIBezierPath() | |
node3bezierPath.addArc(withCenter: center, radius: 100, startAngle: 3.92, endAngle: 5.48, clockwise: true) | |
node3bezierPath.addLine(to: center) | |
let node3 = SKShapeNode(path: node3bezierPath.cgPath) | |
node3.strokeColor = SKColor.green | |
node3.fillColor = SKColor.green | |
self.addChild(node3) | |
// node4 | |
let node4bezierPath = UIBezierPath() | |
node4bezierPath.addArc(withCenter: center, radius: 100, startAngle: 5.48, endAngle: 0.78, clockwise: true) | |
node4bezierPath.addLine(to: center) | |
let node4 = SKShapeNode(path: node4bezierPath.cgPath) | |
node4.strokeColor = SKColor.yellow | |
node4.fillColor = SKColor.yellow | |
self.addChild(node4) | |
} | |
func rotate(angle : CGFloat, animated : Bool) { | |
var rotateAction : SKAction! | |
if animated { | |
rotateAction = SKAction.rotate(byAngle: angle, duration: 0.6) | |
} | |
else { | |
rotateAction = SKAction.rotate(byAngle: angle, duration: 0) | |
} | |
self.run(rotateAction) | |
} | |
} | |
var nyanCat = FourColorCircle() | |
//Basic dimensions that we will use more later | |
let frame = CGRect(x: 0, y: 0, width: 640, height: 512) | |
let midPoint = CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0) | |
//Create a scene, add something to it | |
var scene = SKScene(size: frame.size) | |
//let nyanCat = SKSpriteNode(imageNamed: "Nyancat") | |
nyanCat.position = midPoint | |
nyanCat.setScale(1.0) | |
scene.addChild(nyanCat) | |
//Set up the view and show the scene | |
let view = SKView(frame: frame) | |
view.presentScene(scene) | |
XCPlaygroundPage.currentPage.liveView = view | |
scene.physicsWorld.gravity = CGVector(dx: 0, dy: 0.0) | |
nyanCat.physicsBody = SKPhysicsBody(circleOfRadius: 60 / 2.0) | |
nyanCat.physicsBody?.allowsRotation = true | |
nyanCat.physicsBody?.friction = 0.9 | |
nyanCat.physicsBody?.applyAngularImpulse(0.04) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment