多次检测到碰撞。 [英] Collision detected multiple times.

查看:103
本文介绍了多次检测到碰撞。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a flappy bird game. I seem to be having a problem when my bird passes each wall/collects a coin. There are 2 problems. 1 the game lags for a millisecond after collecting. 2 My bird seems to have 2 or even 3 collisions each time creating a score of 2 or 3, I cannot get my head around this!

我的鸟是5个纹理动画,物理物体包裹着复杂的形状,并带有以下纹理:bird.texture!代码类型。

My bird is a 5 texture animation, with physics body that wraps around its complex shape with the texture: bird.texture! type of code.

我已经尝试了4天,现在这让我的应用程序大为中断!

I have been trying to figure this out for 4 days now its put the breaks on my app big time! Please Helpp!!!

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


推荐答案

虽然还不是很完美,但我还是找到了答案……我的鸟的身体从

I sort of found the answer to this, though it is not perfect... the physicsBody of my bird was changed from

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发生两次碰撞。这可能不是很清楚的说明,但是如果有人遇到相同的问题,请不要犹豫!这是制作我的飞鸟应用程序的最大挫折!

The new path I created was a much simpler path which basically had a square front. So it made it impossible for the shape to collide with my scoreNode twice. This may not be very clear instructions but if anyone has the same problems don't hesitate to ask! This has been the biggest set back of making my flappy bird app!

这篇关于多次检测到碰撞。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆