Post

Replies

Boosts

Views

Activity

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") } } } }
1
0
524
Apr ’22
How do I change the textures of nodes once they are placed?
I’m currently working on a matching game and when the user touches the nodes (Fruit match cards) I want them to display different images when a user clicks the nodes (Fruit match cards). This 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 = 120                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, with event: UIEvent?) {         self.view?.isMultipleTouchEnabled = false      let touch = touches.first   let positionInScene = touch!.location(in: self)   let touchedCardOneNode = self.atPoint(positionInScene)      if let name = touchedCardOneNode.name {          if name == "FruitMatchCard1" {           let newTexture = SKTexture(imageNamed: "Apple")       FruitMatchCard1.init(texture: newTexture)          }      }      let touchTwo = touches.first     let positionInSceneTwo = touch!.location(in: self)     let touchedCardTwoNode = self.atPoint(positionInScene)           if let name = touchedCardTwoNode.name {               if name == "FruitMatchCard2" {                 FruitMatchCard2.init(imageNamed: "Banana")              }          }        let touchThree = touches.first       let positionInSceneThree = touch!.location(in: self)      let touchedCardThreeNode = self.atPoint(positionInScene)            if let name = touchedCardThreeNode.name {                if name == "FruitMatchCard3" {                            FruitMatchCard3.init(imageNamed: "Apple")                }            }        let touchFour = touches.first   let positionInSceneFour = touch!.location(in: self)   let touchedCardFourNode = self.atPoint(positionInScene)      if let name = touchedCardFourNode.name {         if name == "FruitMatchCard4" {                 FruitMatchCard4.init(imageNamed: "Banana")          }      }        } override func touchesEnded(_ touches: Set, with event: UIEvent?) {             } I’m trying to change the textures of the nodes in this part of the code. Inside the “if name == “FruitMatchCard” { } “ part of the code. However, when I launch the Xcode simulator the node’s textures aren't changing. Any advice on how I can do this? Thanks!
1
0
794
Mar ’22