Last active
August 29, 2015 14:05
-
-
Save tado/6065f25cb8fa5e369b2c to your computer and use it in GitHub Desktop.
physics in playground
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
// This code worked width Xcode 6 Beta 6 | |
import SpriteKit | |
import XCPlayground | |
let width:CGFloat = 480.0 | |
let height:CGFloat = 800.0 | |
let canvasWidth: UInt32 = UInt32(width) | |
let canvasHeight: UInt32 = UInt32(height) | |
let view:SKView = SKView(frame: CGRectMake(0, 0, width, height)) | |
let scene:SKScene = SKScene(size: CGSizeMake(width, height)) | |
scene.backgroundColor = SKColor(red: 0, green:0, blue:0, alpha:1) | |
scene.physicsWorld.gravity = CGVectorMake(0, -5) | |
view.presentScene(scene) | |
let physicsBody = SKPhysicsBody(edgeLoopFromRect:CGRect(x:0, y:0, width:width, height:height)) | |
scene.physicsBody = physicsBody; | |
for var i = 0; i < 100; i++ { | |
let shape = SKShapeNode(circleOfRadius:10) | |
shape.fillColor = SKColor(red:0, green:0.5, blue:1.0, alpha:1.0) | |
shape.lineWidth = 2 | |
shape.position = CGPoint(x:CGFloat(arc4random_uniform(canvasWidth)), y:CGFloat(canvasHeight - arc4random_uniform(200))) | |
scene.addChild(shape) | |
shape.physicsBody = SKPhysicsBody(circleOfRadius: shape.frame.size.width/2) | |
shape.physicsBody.friction = 0.01 | |
shape.physicsBody.restitution = 0.95 | |
shape.physicsBody.mass = 1.0 | |
shape.physicsBody.allowsRotation = true | |
} | |
XCPShowView("my view", view) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment