class GameScene: SKScene, SKPhysicsContactDelegate {
let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png"),
]
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")
let blueBallCategory: UInt32 = 0x1 << 0
let greenBallCategory: UInt32 = 0x1 << 1
let realRedBallCategory: UInt32 = 0x1 << 2
let redRectangleCategory: UInt32 = 0x1 << 3
let blueRectangleCategory: UInt32 = 0x1 << 4
let greenRectangleCategory: UInt32 = 0x1 << 5
override func didMove(to view: SKView) {
spawnBallsandRectangles()
physicsWorld.contactDelegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
physics()
}
func didBegin(_ contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == blueBallCategory
&& secondBody.categoryBitMask == redRectangleCategory {
print("dead")
}
}
func spawnBallsandRectangles() {
let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: 0, y: 250)
ball.size = CGSize(width: 70, height: 70)
balls[0].physicsBody?.categoryBitMask = blueBallCategory
balls[1].physicsBody?.categoryBitMask = greenBallCategory
balls[2].physicsBody?.categoryBitMask = realRedBallCategory
balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
redRectangle.position = CGPoint(x: 0, y: -600)
redRectangle.size = CGSize(width: 200, height: 20)
redRectangle.physicsBody?.categoryBitMask = redRectangleCategory
blueRectangle.position = CGPoint(x: -200, y: -600)
blueRectangle.size = CGSize(width: 200, height: 20)
blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory
greenRectangle.position = CGPoint(x: 200, y: -600)
greenRectangle.size = CGSize(width: 200, height: 20)
greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory
self.addChild(ball)
self.addChild(redRectangle)
self.addChild(blueRectangle)
self.addChild(greenRectangle)
}
func physics() {
for ball in balls {
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
}
redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
redRectangle.physicsBody?.affectedByGravity = false
redRectangle.physicsBody?.isDynamic = false
blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
blueRectangle.physicsBody?.affectedByGravity = false
greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
greenRectangle.physicsBody?.affectedByGravity = false
}
}
我想要的藍色球(ball[0]
)打redRectangle
節點和觸發碰撞檢測。如果在藍色球落在紅色矩形上時發現碰撞,我將其編程爲控制檯print ("dead")
。它不這樣做。我無法讓我的應用程序來檢測碰撞
注:要麼是藍色,紅色或綠色球從屏幕和土地上的藍色,紅色或綠色的長方形頂部下降。但到目前爲止,我只設置了藍色球和紅色矩形的檢測碰撞代碼。我希望有人能夠幫助,非常感謝。