首页 > 解决方案 > 碰撞处理在 iOS 13 中不起作用

问题描述

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

苹果的示例代码:

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 中有我不知道的更新功能?

谢谢你的帮助!

标签: iosswiftsprite-kit

解决方案


我创建了一个新的 Xcode 项目并粘贴了问题中的代码,它最终奏效了。

我不知道是哪个项目文件导致了这个问题。

谢谢您的帮助!


推荐阅读