user7875032
user7875032

Reputation:

Coding a SpriteKit Game

Hey everyone my question is relatively simple, how to i get my two blocks switch positions every time i tap the left side of the screen... like if i tap once the two blocks switch positions, then if i tap again they go back to their initial positions(switch back in a sense) and then they continue doing that every tap.

The following image contains my game scene, ignore the blue and yellow blocks, the initial location of the red block is x: 0, y: 475, the initial location of the green block is x: 0, y: 550

Screenshot

Again i need help with switching the block positions every time i touch the left side of the screen.

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

Upvotes: 1

Views: 64

Answers (1)

Alain T.
Alain T.

Reputation: 42133

How about this:

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

Then you won't have to hard code coordinates in multiple places.

Upvotes: 1

Related Questions