Skip to content

Instantly share code, notes, and snippets.

@fluidityt
Last active June 14, 2017 06:42
Show Gist options
  • Save fluidityt/a2889491177318c0316a111ac554ffef to your computer and use it in GitHub Desktop.
Save fluidityt/a2889491177318c0316a111ac554ffef to your computer and use it in GitHub Desktop.
class GameScene: SKScene {
let ball = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 50))
let table = SKSpriteNode(color: .green, size: CGSize(width: 500, height: 500))
func balled() {
ball.position.y += 100
let pb = SKPhysicsBody(rectangleOf: ball.size)
pb.usesPreciseCollisionDetection = true
ball.physicsBody = pb
addChild(ball)
}
func tabled() {
let pb = SKPhysicsBody(rectangleOf: table.size)
pb.affectedByGravity = false
pb.allowsRotation = false
pb.isDynamic = false
pb.usesPreciseCollisionDetection = true
table.physicsBody = pb
addChild(table)
}
func keepBallOnTable() {
if ball.frame.minY < table.frame.maxY {
ball.position.y = table.frame.maxY + ball.size.height / 2 + 1
}
}
override func didMove(to view: SKView) {
balled()
tabled()
}
override func mouseDown(with event: NSEvent) {
keepBallOnTable()
}
override func mouseDragged(with event: NSEvent) {
table.position.y = event.location(in: self).y
}
override func didFinishUpdate() {
keepBallOnTable()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment