【问题标题】:How do I make a node move at a certain position?如何使节点在某个位置移动?
【发布时间】:2015-12-14 15:25:13
【问题描述】:

在我的游戏中,玩家被点击以在屏幕上移动。但是,我已将其设置为在我的 update() 函数中像这样移动:

 if player.position.x == 40 {
   let up = SKAction.moveByX(0,y: 400, duration: 1.0)
    player.runAction(up)
     }

但这似乎不起作用。是否有其他方法可以让我的玩家在达到某个点时向上或向下移动?如有必要,将发布更多代码。

【问题讨论】:

  • 为什么在Update方法中? - 尝试将其移至 touchesBegan 方法当它在更新方法中时,您每秒调用该操作大约 60 次。
  • 我真的不知道。我只是认为它会工作。但它也不想在我的 touchesBegan func 中工作
  • moveByY 不是一个东西,我设置了它,以便通过将 X 坐标设置为 0 来移动 Y
  • 糟糕,我的错 :) 我对 moveToY 感到困惑 :)
  • 很高兴它可以工作 ;-) - 只需根据您的需要更改您的 if 状态,我仍然建议将其从更新中移开 - 以避免性能问题

标签: swift sprite-kit skaction


【解决方案1】:

尝试使用一系列 x 值,然后使用您的操作。使用 == 40 并不是一个很好的选择,因为该位置很少恰好是 40。

 if player.position.x>39 && player.position.x<41 {
 let up = SKAction.moveByX(0,y: 400, duration: 1.0)
 player.runAction(up)
 }

【讨论】:

  • 这太疯狂了,只做if(Int(player.position.x) == 40),或者根据逻辑使用round,因为就像素位置而言,39.1 可能与 40 不同(即编码器希望 39.1 实际到39 不是 40,因此条件不成立)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
相关资源
最近更新 更多