这是我第一次来,我只用了2个月的时间。我正在制作一个目标随机产生的游戏,并且为每个目标触摸它时,其得分会增加+1。现在,我想知道当分数达到选定值时是否有增加目标生成速度的方法。我尝试过使用“ if”,但是我认为只要更改值就启用永远重复,就可以避免。
有解决方案吗? 寻求帮助,如果我的代码不是最好的,抱歉,但是正如我所说的,我只是在学习:)
GameScene类:SKScene {
var time = 5{
didSet {
timeLabel.text = "\(time)"
}
}
var timeLabel: SKLabelNode!
var scoreLabel: SKLabelNode!
var score = 0 {
didSet {
scoreLabel.text = "Score: \(score)"
}
}
override func didMove(to view: SKView) {
timeLabel = SKLabelNode(fontNamed: "Chalkduster")
timeLabel.color = .white
timeLabel.text = "5"
timeLabel.fontSize = 70
timeLabel.horizontalAlignmentMode = .left
timeLabel.position = CGPoint(x:200 , y: 410)
addChild(timeLabel)
scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
scoreLabel.color = .white
scoreLabel.text = "Score: 0"
scoreLabel.horizontalAlignmentMode = .right
scoreLabel.position = CGPoint(x:390 , y: 820)
addChild(scoreLabel)
backgroundColor = SKColor.black
let spawn = SKAction.run({
()in
self.addTarget()
})
var delay = SKAction.wait(forDuration: 2.5)
let spawndelay = SKAction.sequence([spawn,delay])
let spawnforever = SKAction.repeatForever(spawndelay)
self.run(spawnforever)
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
self.time -= 1
if self.time < 1 {
self.timeLabel.alpha = 0.0
timer.invalidate()
}
}
}
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX))
}
func random(min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
func addTarget() {
// Create sprite
let target = SKSpriteNode(imageNamed: "Target")
target.size = CGSize(width: 80, height: 81)
target.name = "targ"
// Determine where to spawn the monster along the Y axis
let actualY = random(min: 70, max: 700)
let actualX = random(min: 50, max: 350)
// Position the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target.position = CGPoint(x: actualX, y: actualY)
// Add the monster to the scene
addChild(target)
target.run(
SKAction.sequence([
SKAction.wait(forDuration: 13.0),
SKAction.removeFromParent()
])
)
if score == 20{
self.enumerateChildNodes(withName: "targ") {
node, stop in
node.run(SKAction.removeFromParent())
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
if touchedNode.name == "targ"{
touchedNode.removeFromParent()
score += 1
break;
}
}
}
}