Swift物理不适用于iOS7

时间:2014-12-08 21:56:23

标签: ios7 swift ios8 sprite-kit skphysicsbody

我有一个使用物理的简单SpriteKit游戏。用Swift编写,在iOS8模拟器中运行良好。 节点停在physicsworld边缘。

但是当在iOS7上运行时,它就会落到谷底。认为它与类别,接触和碰撞位掩码有关。

任何线索?

在此定义类别

struct PhysicsCategory {
    static let None: UInt32 = 0
    static let Edge: UInt32 = 0b1 // 1
    static let Player: UInt32 = 0b10 // 2
    static let Enemy: UInt32 = 0b100 // 4
}

设置世界

physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsWorld.contactDelegate = self
physicsBody!.categoryBitMask = PhysicsCategory.Edge
physicsWorld.gravity = CGVectorMake(0, -9.81)

设置播放器/球/节点

playerNode.physicsBody = SKPhysicsBody(polygonFromPath: path)
playerNode.physicsBody!.contactTestBitMask = PhysicsCategory.Player
playerNode.physicsBody!.dynamic = true
playerNode.physicsBody!.mass = 0.50
playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Enemy | PhysicsCategory.Edge

2 个答案:

答案 0 :(得分:0)

嗯,我没有那个问题。我差不多写了你的代码。我假设playerNode是一个SKShapeNode并在polygonFromPath中使用它的路径你能尝试在iOS7中运行它,看看你是否还有问题吗?

struct PhysicsCategory {
    static let None: UInt32 = 0
    static let Edge: UInt32 = 0b1 // 1
    static let Player: UInt32 = 0b10 // 2
    static let Enemy: UInt32 = 0b100 // 4
}

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    let playerNode = SKShapeNode(ellipseInRect: CGRect(origin: CGPointZero, size: CGSize(width: 10, height: 10)))

    override func didMoveToView(view: SKView) {

        physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        physicsWorld.contactDelegate = self
        physicsBody!.categoryBitMask = PhysicsCategory.Edge
        physicsWorld.gravity = CGVectorMake(0, -9.81)

        self.addChild(playerNode)


        playerNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        playerNode.physicsBody = SKPhysicsBody(polygonFromPath: playerNode.path)
        playerNode.physicsBody!.dynamic = true
        playerNode.physicsBody!.mass = 0.50
        playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
        playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Enemy | PhysicsCategory.Edge
    }

}

答案 1 :(得分:0)

终于搞定了!

更新了Yosemite 10.10.1和Xcode 6.1.1,创建了一个新项目。很奇怪,但现在效果很好。