Created
July 13, 2015 06:53
-
-
Save kazukitanaka0611/4b9d4ac9dff0cd317b6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import SpriteKit | |
class GameScene: SKScene { | |
private var mainCircle: SKSpriteNode! | |
override func didMoveToView(view: SKView) { | |
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) | |
createBall() | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
if let touch: UITouch = touches.first as? UITouch { | |
mainCircle.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 1000)) | |
} | |
} | |
private func createBall() { | |
mainCircle = SKSpriteNode(color: SKColor.whiteColor(), | |
size: CGSize(width: 10, height: 10)) | |
mainCircle.position = CGPoint(x: 200, y: 280) | |
mainCircle.name = "player" | |
mainCircle.physicsBody = SKPhysicsBody(circleOfRadius: 10) | |
mainCircle.physicsBody?.dynamic = true | |
mainCircle.physicsBody?.affectedByGravity = false | |
mainCircle.physicsBody?.mass = 4.0 | |
addChild(mainCircle) | |
for(var loop: Int = 0; loop < 6; loop++) { | |
let angle: CGFloat = ((CGFloat(M_PI) * 2)/6) * CGFloat(loop) | |
var x_offset: CGFloat = cos(angle) | |
var y_offset: CGFloat = sin(angle) | |
x_offset *= 20 | |
y_offset *= 20 | |
let point: SKSpriteNode = SKSpriteNode(color: UIColor.grayColor(), | |
size: CGSize(width: 10, height: 10)) | |
point.position = CGPoint(x: mainCircle.position.x + x_offset, | |
y: mainCircle.position.y + y_offset) | |
point.name = "samllcircle" | |
point.physicsBody = SKPhysicsBody(circleOfRadius: 10) | |
point.physicsBody?.dynamic = true | |
point.physicsBody?.affectedByGravity = true | |
point.physicsBody?.mass = 2/6 | |
addChild(point) | |
let joint: SKPhysicsJointSpring = SKPhysicsJointSpring.jointWithBodyA( | |
mainCircle.physicsBody, | |
bodyB: point.physicsBody, | |
anchorA: mainCircle.position, | |
anchorB: point.position) | |
joint.damping = 2.0 | |
joint.frequency = 9.0 | |
physicsWorld.addJoint(joint) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment