【问题标题】:Weird Spritekit Collision Bug depending on SKSpriteNode position取决于 SKSpriteNode 位置的奇怪 Spritekit 碰撞错误
【发布时间】:2018-04-19 11:49:22
【问题描述】:

我遇到了一个奇怪的Spritekit 问题,我移动的SKSpriteNode 正在通过一个固定的SKSpriteNode,具体取决于固定SKSpriteNode 的位置。

更新:代码在模拟器中有效,但在真实设备上无效。

例子:

将我的 bin SKSpriteNode 放置在位置 x: -500, y: 100 可以正常工作,我的移动 SKSpriteNode 会按预期发生碰撞。

将我的 bin SKSpriteNode 放置在位置 x: -600, y: 100 不起作用,我的移动 SKSpriteNode 不会与 bin 发生碰撞。

使用view.showsPhysics = true 表明在这两种情况下都有物理实体。

-500 和 -508 之间的 x 值按预期工作。我尝试过的所有其他值都不起作用。

与我的其他固定 SKSpriteNodes 的碰撞按预期工作。

enum CollisionTypes: UInt32 {
    case Plane = 1
    case FloorAndRoof = 2
    case OtherObject = 4
    case FinishPoint = 8
}

创建关卡的代码

func createLevel(level: Int) {
    switch level {
    case 1:
        createFloor()
        createRoof()
        createTable(position: CGPoint(x: 750, y: 150))
        createCeilingFan(position: CGPoint(x: 750, y: 560))
        createCeilingFan(position: CGPoint(x: 2000, y: 560))
        createWaterDispenser(position: CGPoint(x: 1500, y: 212))
        createBin(position: CGPoint(x: -500, y: 100)) // THIS IS THE PROBLEM LOCATION

        createFinishPoint(position: CGPoint(x: -500, y: 100))
        break
    case 2:
        createFloor()
        createRoof()
        createTable(position: CGPoint(x: 250, y: 150))
        createCeilingFan(position: CGPoint(x: 750, y: 560))
        createCeilingFan(position: CGPoint(x: 2000, y: 560))
        createWaterDispenser(position: CGPoint(x: 1500, y: 212))
        createBin(position: CGPoint(x: -600, y: 100))

        createFinishPoint(position: CGPoint(x: -300, y: 200))
        break
    default:
        break
    }
}

移动SKSpriteNode

func createPlane() {
    plane = SKSpriteNode(imageNamed: "plane1")
    plane.name = "plane1"
    //plane.position = CGPoint(x: -UIScreen.main.bounds.width + plane.size.width , y: 0)
    plane.position = CGPoint(x: 0, y: 300)
    plane.zPosition = 1
    //plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.texture!.size())
    plane.physicsBody = SKPhysicsBody(texture: planeTexture, size: planeTexture.size())
    plane.physicsBody?.allowsRotation = true
    plane.physicsBody?.categoryBitMask = CollisionTypes.Plane.rawValue
    plane.physicsBody?.collisionBitMask = CollisionTypes.FloorAndRoof.rawValue | CollisionTypes.OtherObject.rawValue // dont collide with finish point
    plane.physicsBody?.contactTestBitMask = CollisionTypes.FloorAndRoof.rawValue | CollisionTypes.OtherObject.rawValue | CollisionTypes.FinishPoint.rawValue
    plane.physicsBody?.usesPreciseCollisionDetection = true
    plane.physicsBody?.isDynamic = true
    plane.physicsBody?.friction = 1
    plane.physicsBody?.restitution = 0
    plane.physicsBody?.mass = 0.1  // customise for different planes
    plane.physicsBody?.angularDamping = 1
    plane.physicsBody?.linearDamping = 0.2 // customise for different planes
    liftFactor = 0.1 // customise for different planes
    addChild(plane)

    flightMode = 4 // dead, should drop to floor and change to mode 0 when at rest
    //print(flightMode)
}

Bin SKSpriteNode 移动的 Plane 应该与之发生碰撞。

func createBin(position: CGPoint) {

    binFront = SKSpriteNode(imageNamed: "binFront")
    binFront.name = "binFront"
    binFront.setScale(0.15)
    binFront.position = position
    binFront.zPosition = 2 // in front of plane
    addChild(binFront)

    binBack = SKSpriteNode(imageNamed: "binBack")
    binBack.name = "binBack"
    binBack.setScale(0.15)
    binBack.position = position
    binBack.zPosition = 0 // behind plane
    addChild(binBack)

    binPhysicsBody = SKSpriteNode(imageNamed: "binPhysicsBody")
    binPhysicsBody.name = "binPhysicsBody"
    binPhysicsBody.physicsBody = SKPhysicsBody(texture: binPhysicsBody.texture!, size: binPhysicsBody.texture!.size())
    binPhysicsBody.setScale(0.15)
    binPhysicsBody.physicsBody?.allowsRotation = false
    binPhysicsBody.physicsBody?.categoryBitMask = CollisionTypes.OtherObject.rawValue
    binPhysicsBody.physicsBody?.collisionBitMask = CollisionTypes.Plane.rawValue
    binPhysicsBody.physicsBody?.contactTestBitMask = CollisionTypes.Plane.rawValue
    binPhysicsBody.physicsBody?.usesPreciseCollisionDetection = false
    binPhysicsBody.physicsBody?.isDynamic = false
    binPhysicsBody.physicsBody?.friction = 1
    binPhysicsBody.physicsBody?.restitution = 0
    binPhysicsBody.physicsBody?.mass = 50
    binPhysicsBody.position = position
    binPhysicsBody.zPosition = 0
    addChild(binPhysicsBody)

}

【问题讨论】:

  • 你有图片吗? Olso 碰撞对象,我的怀疑是与其他对象发生碰撞
  • @SimonePistecchia - 是的,我的大多数节点都有图像。此外,我将 bin 节点从其他物理体移开,但它仍然没有碰撞。但我刚刚发现碰撞确实在模拟器中起作用,但在我的两个物理设备上不起作用。
  • 这张图片来自您的设备吗?模拟器在很多情况下的行为与设备不同,因此来自 sim 的图像毫无价值

标签: ios swift sprite-kit collision


【解决方案1】:

我仍然不知道问题的原因,但我通过更改用于创建我的物理体的图像解决了这个问题。我在垃圾箱上使用了稍厚的“壁厚”。

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多