SpriteKit:Sprites正在相互移动,并且已经设置了physicsBody

时间:2016-10-03 11:15:11

标签: swift sprite-kit collision skphysicsbody

所以我创造了一个移动瓷砖的玩家和围绕地图的墙,以保持玩家在操场上。两者都有物理身体。我的猜测是,我的运动员运动不正确,因此球员会进入墙壁。让我告诉你我的代码:

所以这是玩家的物理实体:

self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Player
self.physicsBody!.contactTestBitMask = PhysicsCategory.House
self.physicsBody!.collisionBitMask = PhysicsCategory.Wall

这就是墙的物理学:

self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.categoryBitMask = PhysicsCategory.Wall
self.physicsBody!.contactTestBitMask = 0
self.physicsBody!.collisionBitMask = PhysicsCategory.Player
self.physicsBody!.isDynamic = false

它们都继承自Objects类:

self.physicsBody = SKPhysicsBody(rectangleOf: size)
self.physicsBody!.affectedByGravity = false
self.name = name

所以,如果原因,请让我给你我的代码,让我知道玩家的动作:

func move(direction: String, width: CGFloat){
    //Choosing the direction based of the users touch
    switch direction{
        case "South":
        //Decreasing the position of the player by 1 Tile
        pos.y -= width

        //Flipping the picture the right way to make the player look in the right direction
        yScale = 1.0

        //Rotates the picture
        let rotateAction = SKAction.rotate(toAngle: -1.57, duration: 0.0)
        run(rotateAction)

       //All the other directions just change the x and y value of pos corresponding to the direction
    }

    //Moves the player to the calculated position
    let moveAction =  SKAction.move(to: pos, duration: 0.0)
    run(moveAction){
        //Sound
    }

编辑:

PhysicsCategory.PlayerPhysicsCategory.Wall

的值
struct PhysicsCategory{
   static let Wall:UInt32 = 0x1 << 0
   static let Player:UInt32 = 0x1 << 1
   static let House:UInt32 = 0x1 << 2
}

编辑2:

主要问题是与SKAction的碰撞正常工作

2 个答案:

答案 0 :(得分:4)

你没有真正描述过你的问题,除了“精灵正在相互移动......”和“所以玩家会闯入墙壁”

要验证您的所有联系人和collsions,请在此处查看我的答案:iOS SpriteKit - collisions and contacts not working as expected并尝试实现checkPhysics()函数并在您认为已设置所有物理机构及其相互作用。

你确定:

  • 玩家和墙壁实际上有物理机构?
  • 场景是SKPhysicsContactDelegate
  • 您已设置physicsWorld.contactDelegate = self
  • 您已经实现了一种可选方法    SKPhysicsContactDelegate
    • didBeginContact
    • didEndcontact

这是否正确:

  • 您希望玩家与墙碰撞吗?
  • 你想让墙碰撞玩家吗?
  • 您希望在播放器时收到通知(didBegincontact) 和房子碰?
  • 如果墙壁碰到任何东西,您不希望收到通知吗?

答案 1 :(得分:3)

此处未在问题中列出,但未发生碰撞联系的原因是因为方法didBeginContact已更改为didBegin