gpt4 book ai didi

swift - 带有 SpriteKit 的点系统

转载 作者:行者123 更新时间:2023-11-30 12:01:19 25 4
gpt4 key购买 nike

我正在制作一个游戏,其中我有一个球(玩家),应该从屏幕的一侧到达另一侧。在两侧我都有一个 SKSpriteNode,球需要接触它才能得分。当球接触一侧的 SKSpriteNode 时,球需要到达另一侧并接触另一个 SKSpriteNode 才能得分。等等。我怎样才能使游戏开始时,顶部的 Sprite 节点处于事件状态,而当触摸该节点时,底部的 Sprite 节点处于事件状态?

我制作了一个“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()
}
}

最佳答案

使用categoryBitMask启用事件主体:

当您希望 goalTop 处于事件状态时:

goalTop.categoryBitMask = category.Goal
goalBottom.categoryBitMask = 0

当您希望 goalBottom 处于事件状态时:

goalTop.categoryBitMask = 0
goalBottom.categoryBitMask = category.Goal

注意:category.Goal 是您用作类别的任何数字

关于swift - 带有 SpriteKit 的点系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47186924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com