Проблема в том, что вы определяете один 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, поэтому я прошу прощения, если мое форматирование не идеально.