碰撞处理在iOS 13中不起作用

时间:2019-09-28 20:55:04

标签: ios swift sprite-kit

我目前正在尝试与Xcode 11(iOS 13)中的碰撞处理相处。 我的问题是,即使Apple的示例代码似乎也不适用于我。 (https://developer.apple.com/documentation/spritekit/skphysicsbody/about_collisions_and_contacts

Apple的示例代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {


    override func didMove(to view: SKView) {

        let ballRadius: CGFloat = 20
        let redBall = SKShapeNode(circleOfRadius: ballRadius)
        redBall.fillColor = .red
        redBall.position = CGPoint(x: 280, y: 320)
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: ballRadius)

        let blueBall = SKShapeNode(circleOfRadius: ballRadius)
        blueBall.fillColor = .blue
        blueBall.position = CGPoint(x: 360, y: 320)
        blueBall.physicsBody = SKPhysicsBody(circleOfRadius: ballRadius)

        var splinePoints = [CGPoint(x: 0, y: 300),
                            CGPoint(x: 100, y: 50),
                            CGPoint(x: 400, y: 110),
                            CGPoint(x: 640, y: 20)]
        let ground = SKShapeNode(splinePoints: &splinePoints,
                                 count: splinePoints.count)
        ground.physicsBody = SKPhysicsBody(edgeChainFrom: ground.path!)
        ground.physicsBody?.restitution = 0.75

        redBall.physicsBody?.collisionBitMask = 0b0001
        blueBall.physicsBody?.collisionBitMask = 0b0010
        ground.physicsBody?.categoryBitMask = 0b0001        

    }




    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

运行代码时,两个球都与曲线碰撞。

SpriteKit是否有我做错的事情或者我不知道的更新功能?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我创建了一个新的Xcode项目,并粘贴了问题中的代码,该代码最终成功了。

我不知道是哪个项目文件引起了问题。

感谢您的帮助!