【问题标题】:Spritekit Coordinate System unexpected positioningSpritekit 坐标系意外定位
【发布时间】:2017-11-20 23:05:59
【问题描述】:

我有 AttackArea、Player 和 GameScene 类。 我想实例化一个新的 AttackArea 对象并将其放置在玩家附近,具体取决于玩家面对的情况。现在我遇到了正确定位的问题。如果我将 AttackArea 添加为 GameScene 的子项,则定位将按预期工作。但如果我这样做,AttackArea 不会随着 Player 移动。否则,如果我将 AttackArea 添加为 Player 的子项,它会与 Player 一起移动。这正是我想要的。这里的问题是 AttackArea 的位置现在离 Player 很远。这是 Player 类中的代码:

func attack(){
    let attack = AttackArea(color: .red, size: CGSize(width: self.frame.width, height: self.frame.height / 2))
    var animation = ""
    switch playerFacing{
    case .back:
        attack.position = CGPoint(x: self.position.x, y: self.position.y + 40)
        animation = Constants.Actions.playerAttackBack
    case .front:
        attack.position = CGPoint(x: self.position.x, y: self.position.y - 40)
        animation = Constants.Actions.playerAttackFront
    case .left:
        attack.position = CGPoint(x: self.position.x - 40, y: self.position.y)
        animation = Constants.Actions.playerAttackLeft
    case .right:
        attack.position = CGPoint(x: self.position.x + 40, y: self.position.y)
        animation = Constants.Actions.playerAttackRight
    case .none:
        break
    }
    attack.zPosition = self.zPosition + 1
    attack.setup()
    if animation != ""{
        self.run(SKAction(named: animation)!)
    }
    self.addChild(attack)
}

第一张图是当 AttackArea 是 GameScene 的子节点时的情况。定位很好,但我希望它是 Player 的孩子。

第二张图是当 AttackArea 是 Player 的子节点时的定位。右上角红色方块为 AttackArea,红色圆圈为 Player。

在这种情况下,为什么 AttackArea 离 Player 这么远?除了 AttackArea 是 Player 的子项之外,我如何才能获得与第一张图片相同的结果?

【问题讨论】:

  • 如果你把physicsBody从攻击区域拿开会发生什么。离玩家还那么远吗?
  • 我试过了,但没有任何改变。 AttackArea 仍然出现在很远的地方。

标签: swift sprite-kit coordinate


【解决方案1】:

如果你改变定位会发生什么

switch playerFacing{
    case .back:
        attack.position = CGPoint(x: 0, y: 0 + 40)
        animation = Constants.Actions.playerAttackBack
    case .front:
        attack.position = CGPoint(x: 0, y: 0 - 40)
        animation = Constants.Actions.playerAttackFront
    case .left:
        attack.position = CGPoint(x: 0 - 40, y: 0)
        animation = Constants.Actions.playerAttackLeft
    case .right:
        attack.position = CGPoint(x: 0 + 40, y: 0)
        animation = Constants.Actions.playerAttackRight
    case .none:
        break
    }

我怀疑您的玩家(例如)可能位于 pos 500、500,而您正在将这些值重新添加到攻击位置,所以现在它的位置是 1000、1040

【讨论】:

  • 很高兴它为你解决了,你有一个好看的游戏
猜你喜欢
  • 2016-03-08
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多