也许是这样的:
import SpriteKit
class Player:SKSpriteNode {
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var gameStarted = false
var player = Player(texture: nil, color: UIColor.brownColor(), size: CGSize(width: 50, height: 100))
var levelTimerLabel = SKLabelNode()
var levelTimerValue: Int = 0 {
didSet {
if levelTimerValue == 10 { self.increaseScore(withDelay: 0.1)}
levelTimerLabel.text = "\(levelTimerValue)m"
}
}
override func didMoveToView(view: SKView) {
self.player.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
addChild(self.player)
levelTimerLabel.zPosition = 200
levelTimerLabel.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
levelTimerLabel.text = "\(levelTimerValue)m"
addChild(levelTimerLabel)
}
//MARK: SCORELABEL METHOD
func increaseScore(withDelay delay:NSTimeInterval = 0.5) {
let block = SKAction.runBlock({[unowned self] in
self.levelTimerValue += 1 // Swift 3 yo
})
let wait = SKAction.waitForDuration(delay)
let sequence = SKAction.sequence([wait,block])
self.runAction(SKAction.repeatActionForever(sequence), withKey: "countUp")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameStarted == false {
gameStarted = true
startWorld()
increaseScore()
}
player.physicsBody?.velocity = CGVectorMake(0, 0)
player.physicsBody?.applyImpulse(CGVectorMake(0, 150)) // Jump Impulse
}
func startWorld(){
print("startWold method invoked")
}
}
当levelTimerValue 达到 10 时,countUp 操作将被替换为新操作,速度将提高 5 倍。我想这就是你想要达到的目标。我稍微修改了您的代码以避免强引用循环,删除了不需要的 update: 方法调用以及一些小事情。另请注意,现在您没有名为“wait”的 SKAction 属性。该操作现在在本地创建。
编辑:
根据您的 cmets,您可以像这样暂停计时器:
func pauseTimer(){
if let countUpAction = self.actionForKey("countUp") {
countUpAction.speed = 0.0
}
}
取消暂停将是相同的...只需将 countUpAction.speed 设置为 1.0
如果您想暂停整个游戏,请查看this SO question。看看@Knight0fDragon 的回答。这是一个不错的方法,而且 IMO,它比完全暂停场景或视图更灵活。
我建议您不要使用字符串“countUp”,而是像这样创建一个常量:
let kCountUpActionKey = "countUp"
这样您在重复使用此操作键时不会出现拼写错误。