gpt4 book ai didi

swift - 生成多个 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-28 07:40:44 24 4
gpt4 key购买 nike

所以我正在制作一个非常像四个正方形的游戏,除了在选择一个正方形之前你有 20 个 Sprite 要放置。在执行正方形决策之前,我只需要有关如何通过点击屏幕 20 次(每次都是不同的 Sprite )来添加多个不同的 Sprite 的帮助。到目前为止,我的代码只适用于一个 Sprite ,但会立即决定选择哪个方 block 。如果有人可以提供帮助,我们将不胜感激!

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
}

}

最佳答案

问题是您正在定义一个单个 character 节点,如下所示:

var character:SKSpriteNode!

要获得 20 个 Sprite ,您可以做的是用这样的东西代替:

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
}

}

如果您需要再次访问字符,请执行以下操作:

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

希望对您有所帮助!如果有任何需要澄清的地方,请发表评论。

我是 stackoverflow 的新手,所以如果我的格式不完美,我深表歉意。

关于swift - 生成多个 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52339691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com