SKSpriteNode掉出屏幕

时间:2018-07-16 16:52:29

标签: ios swift sprite-kit skspritenode

有人可以帮我吗,我创建了一个名为{SpriteKit)的player节点,一旦设置了它的物理特性,它就会掉到屏幕上。

let player = SKSpriteNode(imageNamed: "car")
player.position.x = -300
player.zPosition = 1
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
player.physicsBody.categoryBitMask = 1
addChild(player)

我已经尝试添加

affectedByGravity 

但是对玩家和敌人来说都是相同的结果。

1 个答案:

答案 0 :(得分:0)

如果我明白了,您希望您的汽车不受重力影响。 如果是这样,您可能想设置“世界”的物理原理,例如:

    func setWorld() {
        self.backgroundColor = UIColor.white

        //This should do the trick!
        physicsWorld.gravity = CGVector(dx:0, dy:0)

        //This is useful if you want to create wall along the borders    
        let frame = CGRect(x: self.frame.minX, y: self.frame.minY, width: self.frame.width, height: self.frame.height)
        //here you set the borders as walls
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

        physicsBody?.friction = 0
        physicsBody?.restitution = 1
        physicsBody?.linearDamping = 0
    }

并在didMove中调用此函数