gpt4 book ai didi

swift - 多次检测到碰撞。

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:41 24 4
gpt4 key购买 nike

我正在制作一个飞扬的小鸟游戏。当我的鸟穿过每堵墙/收集硬币时,我似乎遇到了问题。有2个问题。 1 游戏在收集后滞后一毫秒。 2 我的鸟似乎每次碰撞 2 次甚至 3 次,得分为 2 或 3,我无法解决这个问题!

我的鸟是一个 5 纹理动画,物理体包裹着它的复杂形状,纹理为:bird.texture!代码类型。

我已经尝试解决这个问题 4 天了,现在它让我的应用程序大放异彩!请帮忙!!!

func createScene(){

let bird1 = SKTexture(imageNamed: "1")
let bird2 = SKTexture(imageNamed: "2")
let bird3 = SKTexture(imageNamed: "3")
let bird4 = SKTexture(imageNamed: "4")
let bird5 = SKTexture(imageNamed: "5")



let birdAnimation = SKAction.repeatForever(SKAction.animate(with: [bird1, bird2, bird3, bird4, bird5], timePerFrame: 0.1))
let flyForever = SKAction.repeatForever(birdAnimation)

bird = SKSpriteNode(texture: bird1)
bird = CGSize(width: 65, height: 65)
bird = CGPoint(x: self.frame.width / 1.5 - bird, y: self.frame.height / 2)
bird(flyForever, withKey: "birdFly")

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height))


bird.physicsBody?.categoryBitMask = physicsCategory.bird
bird.physicsBody?.collisionBitMask = physicsCategory.ground | physicsCategory.wall
bird.physicsBody?.contactTestBitMask = physicsCategory.ground | physicsCategory.wall | physicsCategory.score
bird?.affectedByGravity = false
bird.physicsBody?.isDynamic = true


self.addChild(bird)


func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == physicsCategory.score && secondBody.categoryBitMask == physicsCategory.bird{



score += 1
scoreLabel.text = "\(score)"
firstBody.node?.removeFromParent()

run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false))

if score > UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()


}

}
else if firstBody.categoryBitMask == physicsCategory.bird && secondBody.categoryBitMask == physicsCategory.score{

score += 1
scoreLabel.text = "\(score)"
secondBody.node?.removeFromParent()

run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false))

if score > UserDefaults().integer(forKey: "HIGHSCORE") {
saveHighScore()


}

}


func createWalls(){



let scoreNode = SKSpriteNode(imageNamed: "bird")
scoreNode.size = CGSize(width: 40, height: 40)
scoreNode.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2)
scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.isDynamic = false
scoreNode.physicsBody?.categoryBitMask = physicsCategory.score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = physicsCategory.bird

最佳答案

我找到了这个问题的答案,虽然它并不完美......我的鸟的 physicsBody 已从

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height))

 let path = CGMutablePath()
path.addLines(between: [CGPoint(x: -8, y: -28),
CGPoint(x: -30, y: 9), CGPoint(x:-11, y: 14), CGPoint(x: -10, y: 27),
CGPoint(x: 26, y: 22), CGPoint(x: 32, y: 20),
CGPoint(x: 30, y: 14), CGPoint(x: 23, y: -17), CGPoint(x: 15, y: -31)])
path.closeSubpath()
bird.physicsBody = SKPhysicsBody(polygonFrom: path)

我创建的新路径是一条简单得多的路径,它的正面基本上是方形的。所以它使得形状不可能与我的 scoreNode 碰撞两次。这可能不是很清楚的说明,但如果有人有同样的问题,请不要犹豫,问!这是制作我的 flappy bird 应用程序的最大挫折!

关于swift - 多次检测到碰撞。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41153181/

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