-
-
Save fluidityt/a2889491177318c0316a111ac554ffef to your computer and use it in GitHub Desktop.
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
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