我有一个玩家和一面墙,每当玩家走进墙壁时它就会推开它。墙是静态的,玩家是动态的,任何人都知道如何解决这个问题?
玩家代码:
player.texture = SKTexture(imageNamed: "player")
player.position = CGPoint(x: 0 + player.frame.width / 2,y: self.frame.height + player.frame.height / 2)
player.zPosition = 2
player.physicsBody?.affectedByGravity = true
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.pinned = false
player.physicsBody?.mass = 0.18
player.physicsBody?.restitution = 0
移动播放器的代码:
let moveForward = SKAction.moveByX(1, y: 0, duration: 0.01)
墙代码:
backWall = SKSpriteNode(imageNamed: "up")
backWall.size = CGSize(width: 70, height: 70)
backWall.position = CGPoint(x: 0 + 35, y: ground.frame.height)
backWall.zPosition = 3
backWall.physicsBody?.categoryBitMask = PhysicsCategory.backWall
backWall.physicsBody?.contactTestBitMask = PhysicsCategory.backWall
backWall.physicsBody?.collisionBitMask = PhysicsCategory.backWall
backWall.physicsBody?.dynamic = false
backWall.physicsBody?.affectedByGravity = false
backWall.physicsBody?.allowsRotation = false
backWall.physicsBody?.friction = 1.0
backWall.physicsBody = SKPhysicsBody(rectangleOfSize: backWall.size)
答案 0 :(得分:1)
当您实例化SKSpriteNode
时,其physicsBody
为零。
所以,这些代码行:
backWall.physicsBody?.categoryBitMask = PhysicsCategory.backWall
backWall.physicsBody?.contactTestBitMask = PhysicsCategory.backWall
backWall.physicsBody?.collisionBitMask = PhysicsCategory.backWall
backWall.physicsBody?.dynamic = false
backWall.physicsBody?.affectedByGravity = false
backWall.physicsBody?.allowsRotation = false
backWall.physicsBody?.friction = 1.0
默默地被忽略。
您可能需要先指定SKPhysicsBody
:
backWall.physicsBody = SKPhysicsBody(rectangleOfSize: backWall.size)
backWall.physicsBody?.categoryBitMask = PhysicsCategory.backWall
backWall.physicsBody?.contactTestBitMask = PhysicsCategory.backWall
backWall.physicsBody?.collisionBitMask = PhysicsCategory.backWall
backWall.physicsBody?.dynamic = false
backWall.physicsBody?.affectedByGravity = false
backWall.physicsBody?.allowsRotation = false
backWall.physicsBody?.friction = 1.0
我没有测试过您的代码,因此您可能需要修复其他部分。特别是,如果player
的{{1}}已正确设置,我担心。