Создание нескольких SKSpriteNode - PullRequest
0 голосов
/ 15 сентября 2018

Итак, я создаю игру, которая очень похожа на четыре квадрата, за исключением того, что у вас есть 20 спрайтов, которые нужно разместить, прежде чем выбрать квадрат.Мне просто нужна помощь о том, как добавить несколько разных спрайтов, коснувшись экрана 20 раз (каждый раз это другой спрайт) перед выполнением квадратного решения.Пока что у меня есть код, который работает только для одного спрайта, но он сразу решает, какой квадрат выбранЕсли кто-то может помочь, это будет оценено, спасибо!

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
    }

}

1 Ответ

0 голосов
/ 29 сентября 2018

Проблема в том, что вы определяете один character узел следующим образом:

var character:SKSpriteNode!

Что вы можете сделать, чтобы получить 20 спрайтов, это заменить накак-то так:

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
    }

}

Если вам когда-нибудь понадобится доступ к character, снова сделайте это:

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

Надеюсь, это помогло!Если что-то требует уточнения, просто прокомментируйте.

Я новичок в stackoverflow, поэтому я прошу прощения, если мое форматирование не идеально.

...