ios - 编码 SpriteKit 游戏

标签 ios iphone swift swift3 sprite-kit

大家好,我的问题相对简单,每次我点击屏幕左侧时如何让我的两个方 block 切换位置...就像我点击一次两个方 block 切换位置,然后如果我再次点击它们回到他们的初始位置(在某种意义上切换回来)然后他们在每次点击时继续这样做。

下图是我的游戏场景,忽略蓝色和黄色方 block ,红色方 block 初始位置为x:0,y:475,绿色方 block 初始位置为x:0,y:550

Screenshot

我每次触摸屏幕左侧时再次需要帮助来切换方 block 位置。

//These are my actions...

let topUnderBlockSwitch = SKAction.moveTo(y: 550, duration: 0.1)
let topAboveBlockSwitch = SKAction.moveTo(y: 475, duration: 0.1)
let bottomUnderBlockSwitch = SKAction.moveTo(y: -475, duration: 0.1)
let bottomTopBlockSwitch = SKAction.moveTo(y: -550, duration: 0.1)


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

  for touch in touches {

    let location = touch.location(in: self)
    if location.x < 0 {

        if greenBlock.position.y == 550 {

            redBlock.run(topUnderBlockSwitch)
            greenBlock.run(topAboveBlockSwitch)

        } else if greenBlock.position.y == 475 {

             redBlock.run(topUnderBlockSwitch)
             greenBlock.run(topAboveBlockSwitch)

        }

    } else if // the rest of the code for the right side...

最佳答案

这个怎么样:

redBlock.run(SKAction.moveTo(y:greenBlock.position.y, duration:0.1))
greenBlock.run(SKAction.moveTo(y:redBlock.position.y, duration:0.1))

这样您就不必在多个地方硬编码坐标。

关于ios - 编码 SpriteKit 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439207/

相关文章:

javascript - 尝试访问本地存储时 javascript/html5 中的 Dom 异常 18

ios - 如果观察者已在 Swift 中注册,如何获取

ios - swift 中的 barTintColor

快速用户通知点击

ios - 从方案中删除实体后的核心数据迁移

ios - 用直接代码替换@selector

ios - 是否可以为 iPhone 设置本地主机 IP?

iOS 11 CoreNFC 如何处理读取错误?

ios - 层旋转

ios - 对成员 'tableView(_:numberOfRowsInSection:)' 的不明确引用 为什么会出现此错误?