有人可以帮助我吗?我创建了一个名为(player
)的 SpriteKit
节点,一旦我设置了它的物理特性,它就会从屏幕上掉下来:
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
但玩家和敌人的结果相同。
最佳答案
如果我明白了,你希望你的车不受重力的影响。 如果是这样,您可能需要设置“世界”的物理特性,例如:
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中调用这个函数
关于ios - SKSpriteNode 脱离屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366664/