Developer0113
Developer0113

Reputation: 19

In SpriteKit how can I have code for tracking the textures of two nodes at a time? As well code to track if all the nodes were matched in time?

I would like to have one piece of code in my app where I can track the textures of two nodes at a time. If the textures of the two nodes don't match I would like them reset back to their original state before the user touched them. On top of that i would like another piece of code for tracking if the particular node's textures were matched under a particular time frame like 90 seconds. Any tips on how I can do something like this for my app? 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")
    matchCardOne.name = "FruitMatchCard1"
    matchCardTwo.name = "FruitMatchCard2"
    matchCardThree.name = "FruitMatchCard3"
    matchCardFour.name = "FruitMatchCard4"
    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)
    matchCardOne.zPosition = 1
    matchCardTwo.zPosition = 1
    matchCardThree.zPosition = 1
    matchCardFour.zPosition = 1
    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)
    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) 
    addChild(background)
    addChild(matchCardOne)
    addChild(matchCardTwo)
    addChild(matchCardThree)
    addChild(matchCardFour)
    addChild(timerText)
}

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")
         }
        }
    }
   }
  }
  }
   }
   }

Upvotes: 0

Views: 66

Answers (0)

Related Questions