无法将PhysicsBody和属性分配给SKSpriteNode吗?

问题描述 投票:-1回答:1

我正在构建一个函数,其中随机变量将在数组中选择4个SKSpritenode中的1个并将其自身分配给它。但是,该随机线尽管按预期显示在屏幕上,但不包含任何Physicalbody属性,因此它不会与其他节点碰撞。下面是我的代码:

func line() {

        redLine = SKSpriteNode(imageNamed: "redline")
        blueLine = SKSpriteNode(imageNamed: "blueline")
        yellowLine = SKSpriteNode(imageNamed: "yellowline")
        greenLine = SKSpriteNode(imageNamed: "greenline")

        let lineArray = [redLine,blueLine,yellowLine,greenLine]
        // Add physics
        lineArray[0].physicsBody?.categoryBitMask = gamePhysics.RedLine
        lineArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueLine
        lineArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowLine
        lineArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenLine

        for i in 0...3 {
            lineArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 10)
            lineArray[i].physicsBody?.collisionBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
            lineArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
            lineArray[i].physicsBody?.affectedByGravity = false
            lineArray[i].physicsBody?.isDynamic = true
        }
        let randomLine:SKSpriteNode! = lineArray.randomElement()
        ...

        self.addChild(randomLine)
        ...

        randomLine.run(SKAction.sequence([moveLine,delay,removeLine]))
    }

所以基本上,当随机线与其他节点碰撞时,什么也没有发生。

这是我的didBegin(联系人)函数:

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

        if firstBody.categoryBitMask == gamePhysics.RedBall && secondBody.categoryBitMask == gamePhysics.RedLine || firstBody.categoryBitMask == gamePhysics.BlueBall && secondBody.categoryBitMask == gamePhysics.BlueLine || firstBody.categoryBitMask == gamePhysics.YellowBall && secondBody.categoryBitMask == gamePhysics.YellowLine || firstBody.categoryBitMask == gamePhysics.GreenBall && secondBody.categoryBitMask == gamePhysics.GreenLine {

            currentScore += 1
            secondBody.node?.removeFromParent()
            print("hit point")

        } else if firstBody.categoryBitMask == gamePhysics.RedLine && secondBody.categoryBitMask == gamePhysics.RedBall || firstBody.categoryBitMask == gamePhysics.BlueLine && secondBody.categoryBitMask == gamePhysics.BlueBall || firstBody.categoryBitMask == gamePhysics.YellowLine && secondBody.categoryBitMask == gamePhysics.YellowBall || firstBody.categoryBitMask == gamePhysics.GreenLine && secondBody.categoryBitMask == gamePhysics.GreenBall {

            currentScore += 1
            firstBody.node?.removeFromParent()
            print("hit point")

        }
    }

任何帮助将不胜感激!编辑:这是球func()。也许SKNode以某种方式包含多个categorybitmask,因此将无法正常工作?

func Ball() {

        ballNode = SKNode()
        redBall = SKSpriteNode(imageNamed: "redball")
        blueBall = SKSpriteNode(imageNamed: "blueball")
        yellowBall = SKSpriteNode(imageNamed: "yellowball")
        greenBall = SKSpriteNode(imageNamed: "greenball")

        let ballArray = [redBall,blueBall,yellowBall,greenBall]
        ...

        // Add physics
        ballArray[0].physicsBody?.categoryBitMask = gamePhysics.RedBall
        ballArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueBall
        ballArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowBall
        ballArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenBall

        for i in 0...3 {
            ballArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 20)
            ballArray[i].physicsBody?.collisionBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
            ballArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
            ballArray[i].physicsBody?.affectedByGravity = false
            ballArray[i].physicsBody?.isDynamic = false
        }

        ballNode.addChild(redBall)
        ballNode.addChild(greenBall)
        ballNode.addChild(yellowBall)
        ballNode.addChild(blueBall)

        self.addChild(ballNode)
    }
swift xcode skspritenode skphysicsbody sknode
1个回答
0
投票

您还记得在场景内设置联系人代表吗?将这行代码放在didMove(to:)中:

self.physicsWorld.contactDelegate = self

[如果这不能解决问题,我将使用didBegin&&语句之间的括号使||中的if条件更加明确。

查看其余的代码,我看不到还有什么可能是错误的,您所做的应能起作用。是否到达了放置在if语句中的print("hit point")

© www.soinside.com 2019 - 2024. All rights reserved.