【问题标题】:SpriteKit: Sprites are moving through each other with a physicsBody already setSpriteKit:精灵在已经设置了物理体的情况下相互移动
【发布时间】:2017-02-11 07:41:22
【问题描述】:

因此,我创建了一个玩家,该玩家基于图块移动,并在地图周围设置一堵墙,以使玩家保持在操场上。两者都有一个physicsBody。我的猜测是,我的玩家动作不正确,所以玩家会撞到墙上。让我向您展示我的代码:

这就是玩家的physicsBody:

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 正常工作的碰撞

【问题讨论】:

  • PhysicsCategory.Player、PhysicsCategory.House 和 PhysicsCategory.Wall 的值是什么?
  • 已编辑。房子是玩家必须去的目标。同样的错误也发生在那里。
  • 我们需要比我更强大的头脑。我的很困惑。
  • 您需要什么信息?

标签: swift sprite-kit collision skphysicsbody


【解决方案1】:

除了“精灵正在相互移动......”和“所以玩家钻进墙壁”之外,您实际上没有描述过您的问题

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

你确定:

  • 玩家和墙实际上有物理实体?
  • 场景是SKPhysicsContactDelegate
  • 您已设置physicsWorld.contactDelegate = self
  • 您已经实现了以下可选方法之一 SKPhysicsContactDelegate:
    • didBeginContact
    • didEndcontact

这是否正确:

  • 你想让玩家撞墙吗?
  • 你想让墙和玩家发生碰撞?
  • 您希望在播放器时收到通知(调用didBegincontact) 和房子接触?
  • 您不想在墙壁接触到任何东西时收到通知?

【讨论】:

  • 是的,我做到了。上面我链接了一个聊天,我上传了一个显示问题的视频。我要看看你的 checkPhysics funktion
  • 嗯,好的——让聊天链接更明显一点。
  • 这是一份关于如何让物理学发挥作用的绝妙清单和清单。希望您正在编写 Apple 的文档!
【解决方案2】:

这里的问题中没有列出,但是没有发生碰撞接触的原因是因为方法didBeginContact变成了didBegin

【讨论】:

  • 碰撞体呢?没问题还是有别的原因?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
相关资源
最近更新 更多