编码SpriteKit游戏

时间:2017-04-16 16:12:08

标签: ios iphone swift swift3 sprite-kit

嘿大家我的问题相对简单,我每次点击屏幕左侧时如何获得两个块切换位置...就像我点击两个块切换位置一样,然后如果我再次点击他们回到他们的初始位置(在某种意义上转回)然后他们继续每次敲击。

下图包含我的游戏场景,忽略蓝色和黄色块,红色块的初始位置是x:0,y:475,绿色块的初始位置是x:0,y:550 < / p>

Screenshot

每次触摸屏幕左侧时,我都需要帮助切换块位置。

//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...

1 个答案:

答案 0 :(得分:1)

这个怎么样:

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

然后你不必在多个地方硬编码坐标。

相关问题