1

So im making a game which is pretty much like four squares except you have 20 sprites to place before a square is chosen. I just need help on how to add multiple different sprites by taping the screen 20 times (each time is a different sprite) before executing the square decision. So far the code I have works for just one sprite but then immediately decides which square is chosen. If anyone could help that would be appreciated, thanks!

import SpriteKit
import GameplayKit

class GameScene: SKScene {
    var character:SKSpriteNode!
    var count:Int = 0
    var number:Int!
    var countDownLabel:SKLabelNode!
    var scoreLabel:SKLabelNode!
    var loss:Int = 0
    var score:Int = 0 {
        didSet {
            scoreLabel.text = "Score: \(score)"
        }
    }

    override func didMove(to view: SKView) {
        character = SKSpriteNode()
        scoreLabel = SKLabelNode(text: "Score: 0")
        scoreLabel.position = CGPoint(x: 300, y: 620)
        scoreLabel.fontSize = 36
        scoreLabel.fontColor = .black
        self.addChild(scoreLabel)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let  touch = touches.first!
        let location = touch.location(in: self)
        character = SKSpriteNode(imageNamed: "shuttle")
        character.position = location
        self.addChild(character)

        number = Int(arc4random_uniform(4)+1)

        if (location.x < 0 && location.y > 0 && number == 1){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x > 0 && location.y > 0 && number == 2){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x < 0 && location.y < 0 && number == 3){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x > 0 && location.y < 0 && number == 4){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else {
            print("you win")
            score += 1
    }

}
ninjaproger
  • 2,024
  • 1
  • 27
  • 26

1 Answers1

0

The problem is you are defining a single character node as follows:

var character:SKSpriteNode!

What you could do to get 20 sprites is replace with something like this:

import SpriteKit
import GameplayKit

class GameScene: SKScene {
    var count:Int = 0
    var number:Int!
    var countDownLabel:SKLabelNode!
    var scoreLabel:SKLabelNode!
    var loss:Int = 0
    var score:Int = 0 {
        didSet {
            scoreLabel.text = "Score: \(score)"
        }
    }

    override func didMove(to view: SKView) {
        scoreLabel = SKLabelNode(text: "Score: 0")
        scoreLabel.position = CGPoint(x: 300, y: 620)
        scoreLabel.fontSize = 36
        scoreLabel.fontColor = .black
        self.addChild(scoreLabel)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        count = count + 1
        let touch = touches.first!
        let location = touch.location(in: self)
        let character = SKSpriteNode()
        character.name = "character\(count)
        character = SKSpriteNode(imageNamed: "shuttle")
        character.position = location
        self.addChild(character)

        number = Int(arc4random_uniform(4)+1)

        if (location.x < 0 && location.y > 0 && number == 1){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x > 0 && location.y > 0 && number == 2){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x < 0 && location.y < 0 && number == 3){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else if (location.x > 0 && location.y < 0 && number == 4){
            character.removeFromParent()
            print("you lose")
            loss += 1
        } else {
            print("you win")
            score += 1
    }

}

If you ever need to access the character again do this:

let number = //the id of the character you want to access
if let character = self.childNode(withName: "character\(number)") {
    //enter code here
}

I hope this helped! If anything needs clarifying just comment.

I'm new to stackoverflow so I apologise if my formatting isn't perfect.