In swift for sprite kit, how can I have a functionality that tracks the touches of two nodes and a time? Along with tracking the textures?

I’m working on a matching game. In my app, I would like a functionality that tracks the touches of two nodes at time. When the user touches the nodes and if the textures displayed after the user touches the nodes match; I don’t want the user to be able to interact with the two nodes anymore.

However, if the textures displayed after the user touches the two nodes do not match. I want the two nodes the user touched to reset back to their original position before the user touched the nodes.

For example, let’s say the user touches the node named “fruit match card1” and it displays the texture of an “apple” then they touch the node named “fruit match card 2” and it also displays the texture of an “apple”. Since, these two nodes display the same texture that match. I don’t want the user to be able to interact with those nodes anymore since they clicked nodes that display the same texture.

However, let’s say the user touched the node named “Fruit match card 1” and it displays the “Apple” texture. Then they touched the node named “Fruit match Card 3” and it displays the “Grapes” texture. Since, these two nodes do not display the same texture I want those nodes to reset back to their original position before the user touched them.

Any advice or help on how I can have those kind of functionalities in my app? Basically having a functionality that tracks the touches of two nodes at a time and also a functionality that will reset the nodes back to their original position of the textures of the two nodes touched do not match? Thanks!

Here is my current code:

import Foundation
import SpriteKit

class EasyScreen: SKScene {





override func didMove(to view: SKView) {

    

    var background = SKSpriteNode(imageNamed: "Easy Screen Background")

    

    let timerText = SKLabelNode(fontNamed: "Arial")

     timerText.fontSize = 40

    timerText.fontColor = SKColor.white

     timerText.position = CGPoint(x: 20, y: 400)

     timerText.zPosition = 1



    

    

    var counter:Int = 90

    

    

    

    timerText.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {

        

        counter-=1

        timerText.text = " Time: \(counter)"

        print("\(counter)")

        

        if counter <= 0{

            

          let newScene = TryAgainScreen(fileNamed: "Try Again Screen")

        newScene?.scaleMode = .aspectFill

        self.view?.presentScene(newScene)

            

          

            

            

        }

        },SKAction.wait(forDuration: 1)])))

    

        

        



    

    

    

    background.position = CGPoint(x: 0, y: 0)

    background.size.width = self.size.width

    background.size.height = self.size.height

    background.anchorPoint = CGPoint(x: 0.5,y: 0.5)

    

    



    

   

    let matchCardOne = SKSpriteNode(imageNamed: "Fruit Match Card")

    let matchCardTwo = SKSpriteNode(imageNamed: "Fruit Match Card")

    let matchCardThree = SKSpriteNode(imageNamed: "Fruit Match Card")

    let matchCardFour = SKSpriteNode(imageNamed: "Fruit Match Card")

    let soundButton = SKSpriteNode(imageNamed: "Sound On")

    

    

    matchCardOne.name = "FruitMatchCard1"

    matchCardTwo.name = "FruitMatchCard2"

    matchCardThree.name = "FruitMatchCard3"

    matchCardFour.name = "FruitMatchCard4"

    soundButton.name = "Sound"

    

    

    matchCardOne.size = CGSize(width: 150, height: 300)

    matchCardTwo.size = CGSize(width: 150, height: 300)

    matchCardThree.size = CGSize(width: 150, height: 300)

    matchCardFour.size = CGSize(width: 150, height: 300)

    soundButton.size = CGSize(width: 75, height: 75)

    

    

    

    matchCardOne.zPosition = 1

    matchCardTwo.zPosition = 1

    matchCardThree.zPosition = 1

    matchCardFour.zPosition = 1

    soundButton.zPosition = 3

    

    

    matchCardOne.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    matchCardTwo.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    matchCardThree.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    matchCardFour.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    soundButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    

    

matchCardOne.position = CGPoint(x: -125, y: 60)

    matchCardTwo.position = CGPoint(x: -125, y: -260)

    matchCardThree.position = CGPoint(x: 70, y: 60)

    matchCardFour.position = CGPoint(x: 70 , y: -260)

   

    soundButton.position = CGPoint(x: -180, y: -600)

    

      

addChild(background)

 addChild(matchCardOne)

    addChild(matchCardTwo)

    addChild(matchCardThree)

    addChild(matchCardFour)

    addChild(timerText)

    

    addChild(soundButton)

}


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




self.view?.isMultipleTouchEnabled = false




let touch = touches.first

let positionInSceneOne = touch!.location(in: self)

let tappedNodes = nodes(at: positionInSceneOne)





for node in tappedNodes{

    

    if let tappedCard = node as? SKSpriteNode {

        

        if tappedCard.name == "FruitMatchCard1" {

           

            tappedCard.texture = SKTexture(imageNamed: "Apple")

            

        }

    

}


}



    let touchTwo = touches.first

   let positionInSceneTwo = touch!.location(in: self)

   let tappedNodesTwo = nodes(at: positionInSceneTwo)

   

 for node in tappedNodesTwo{



 if let tappedCard = node as? SKSpriteNode {

  

  if tappedCard.name == "FruitMatchCard2" {

     

    tappedCard.texture = SKTexture(imageNamed: "Apple")

    

    

    

      

  }

    

    

}

   
}


let touchThree = touches.first

      let positionInSceneThree = touch!.location(in: self)

      let tappedNodesThree = nodes(at: positionInSceneThree)

      

     for node in tappedNodesThree{

     

     if let tappedCard = node as? SKSpriteNode {

         

         if tappedCard.name == "FruitMatchCard3" {

            

           tappedCard.texture = SKTexture(imageNamed: "Grapes")

            

            

             

         }





        }



    
}




let touchFour = touches.first

let positionInSceneFour = touch!.location(in: self)

let tappedNodesFour = nodes(at: positionInSceneFour)

      

     for node in tappedNodesFour{

     

     if let tappedCard = node as? SKSpriteNode {

         

         if tappedCard.name == "FruitMatchCard4" {

            

             tappedCard.texture = SKTexture(imageNamed: "Grapes")

             

         }





        }



    }





}

@wviper3 any suggestions?

This topic was automatically closed after 166 days. New replies are no longer allowed.