我正在制作一个游戏,我有一个球(玩家),假设从一侧到另一个屏幕。在两侧我都有一个SKSpriteNode,球需要触摸得到一分。当球接触侧面的SKSpriteNode时,球需要到达另一侧并触摸另一个SKSpriteNode以获得另一个点。等等。我怎样才能让游戏开始时顶部的spritenode处于活动状态,当触摸它时,底部的spritenode是活动的?
我做了一个" incrementScore"每当球触及"点":
时,每增加一个得分的功能func incrementScore() {
score += 1
scoreLabel.text = String(score)
}
然后我有两个"目标"球需要触及得分:
func createGoals() {
let goalTop = SKSpriteNode(imageNamed: "Goal")
goalTop.name = "Goal"
goalTop.anchorPoint = CGPoint(x: 0.5, y: 0.5)
goalTop.position = CGPoint(x: 0, y: 465)
goalTop.physicsBody = SKPhysicsBody(rectangleOf: goalTop.size)
goalTop.physicsBody?.affectedByGravity = false
goalTop.physicsBody?.isDynamic = false
goalTop.zPosition = 2
self.addChild(goalTop)
let goalBottom = SKSpriteNode(imageNamed: "Goal")
goalBottom.name = "Goal"
goalBottom.anchorPoint = CGPoint(x: 0.5, y: 0.5)
goalBottom.position = CGPoint(x: 0, y: -435)
goalBottom.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width:
goalBottom.size.width - 10, height: goalBottom.size.height - 10))
goalBottom.physicsBody?.affectedByGravity = false
goalBottom.physicsBody?.isDynamic = false
goalBottom.zPosition = 2
self.addChild(goalBottom)
}
此外,我还开始使用didBegin联系人功能:
func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Player" {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Player" && secondBody.node?.name ==
"Point" {
incrementScore()
}
}
答案 0 :(得分:0)
使用categoryBitMask启用活动正文:
当您希望goalTop处于活动状态时:
goalTop.categoryBitMask = category.Goal
goalBottom.categoryBitMask = 0
当您希望goalBottom处于活动状态时:
goalTop.categoryBitMask = 0
goalBottom.categoryBitMask = category.Goal
注意:category.Goal
是您用作类别的数字