gpt4 book ai didi

ios - 我怎样才能停止计时器?

转载 作者:行者123 更新时间:2023-11-29 01:59:12 29 4
gpt4 key购买 nike

我刚开始学习如何为 Apple 编写游戏程序,并且一直在寻找如何停止计时器。在下面的代码中,我想停止我制作的计时器,但我无法停止它。我没有任何线索。请帮我!谢谢!

类游戏:SKScene {

var Ball = SKSpriteNode(imageNamed: "Red.png")
var QuitOption = SKLabelNode()
var ScoreLabel = SKLabelNode()
var timescore = Int()
var timesecond = Int()
var locked = false




override func didMoveToView(view: SKView) {


backgroundColor = SKColor.blackColor() // background for the display

self.physicsWorld.gravity = CGVectorMake(0, -9.8)

let SceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
SceneBody.friction = 0
self.physicsBody = SceneBody


Ball.size = CGSize(width: 120, height: 120)
Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: 60)
Ball.physicsBody?.affectedByGravity = true
Ball.physicsBody?.restitution = 0.1
Ball.physicsBody?.linearDamping = 0
Ball.name = "Ball"

self.addChild(Ball)

QuitOption.text = "Quit"
QuitOption.fontName = "Noteworthy-Light"
QuitOption.fontColor = SKColor.greenColor()
QuitOption.fontSize = 35
QuitOption.position = CGPoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110)
QuitOption.name = "Quit"

addChild(QuitOption)

ScoreLabel = SKLabelNode(fontNamed: "Noteworthy-Light")
ScoreLabel.fontSize = 50 // The + will move it to the right side and - to the left side for more accuracy.
ScoreLabel.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/4 + 400) // position of ScoreLabelNode
ScoreLabel.name = "Score+"
ScoreLabel.hidden = false

self.addChild(ScoreLabel)


}

// Making the ball jump after user touches ball

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


var touch = touches.first as! UITouch
var location = touch.previousLocationInNode(self)
// var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)


if (node.name == "Quit"){

let myScene = GameScene(size: self.size)
myScene.scaleMode = scaleMode
let reveal = SKTransition.fadeWithDuration(1)
self.view?.presentScene(myScene, transition: reveal)

}

if (node.name == "Ball"){

for touch: AnyObject in touches {

let location = touch.locationInNode(Ball)

Ball.physicsBody?.allowsRotation = true
Ball.physicsBody?.velocity = CGVectorMake(0, 0)
Ball.physicsBody?.applyImpulse(CGVectorMake(0, 100))

}


}

if(!self.locked){

self.locked = true

var actionrun = SKAction.waitForDuration(0.5)

var actionwait = SKAction.runBlock({

var stopTimer = false

self.timescore++
self.timesecond++


if self.timesecond == 60 {self.timesecond = 0}

self.ScoreLabel.text = " \(self.timescore/60):\(self.timesecond)"})

ScoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionrun,actionwait])))



}


}

最佳答案

如果我理解你的问题,我不能百分百确定会怎样。但如果“计时器”指的是您的循环操作,您可以这样做:

// This is just running your action with a name "scoreAction"
let loopAction = SKAction.repeatActionForever(
SKAction.sequence([actionrun,actionwait]))
ScoreLabel.runAction(loopAction, withKey: "scoreAction")

稍后在代码中的某个位置,您可以通过以下方式停止此操作:

ScoreLabel.removeActionForKey("scoreAction")

更新:

如果您只想暂停/取消暂停动画,您可以在要暂停的对象上使用 paused 属性。

ScoreLabel.pause = true

很高兴知道 .pause 会影响 Node 和任何子节点。例如,您可以暂停整个场景:

self.pause = true

甚至 SKScene 也是一个“节点”。

关于ios - 我怎样才能停止计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540283/

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