我怎样才能创建一个每两秒触发一次的计时器,这会在我屏幕上的HUD上将分数增加一?这是我对HUD的代码:
@implementation MyScene
{
int counter;
BOOL updateLabel;
SKLabelNode *counterLabel;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
counter = 0;
updateLabel = false;
counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
counterLabel.name = @"myCounterLabel";
counterLabel.text = @"0";
counterLabel.fontSize = 20;
counterLabel.fontColor = [SKColor yellowColor];
counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
counterLabel.position = CGPointMake(50,50); // change x,y to location you want
counterLabel.zPosition = 900;
[self addChild: counterLabel];
}
}
答案 0 :(得分:59)
在Sprite Kit中不要使用 NSTimer
,performSelector:afterDelay:
或Grand Central Dispatch(GCD,即任何dispatch_...
方法),因为这些计时方法会忽略节点& #39; s,场景或视图的paused
状态。此外,您不知道它们在游戏循环中的哪个位置执行会导致各种问题,具体取决于您的代码实际执行的操作。
在Sprite Kit中执行基于时间的内容的唯一两种制裁方法是使用SKScene update:
方法并使用传入的currentTime参数来跟踪时间。
或者更常见的是,您只需使用以等待操作开头的动作序列:
id wait = [SKAction waitForDuration:2.5];
id run = [SKAction runBlock:^{
// your code here ...
}];
[node runAction:[SKAction sequence:@[wait, run]]];
重复运行代码:
[node runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]]];
或者,您也可以使用performSelector:onTarget:
代替runBlock:
,或者如果您需要模仿SKScene customActionWithDuration:actionBlock:
方法并且不知道如何使用update:
将它转发到节点或转发不方便的地方。
有关详细信息,请参阅SKAction reference。
更新:使用Swift的代码示例
Swift 3
let wait = SKAction.wait(forDuration:2.5)
let action = SKAction.run {
// your code here ...
}
run(SKAction.sequence([wait,action]))
Swift 2
let wait = SKAction.waitForDuration(2.5)
let run = SKAction.runBlock {
// your code here ...
}
runAction(SKAction.sequence([wait, run]))
Aund重复运行代码:
runAction(SKAction.repeatActionForever(SKAction.sequence([wait, run])))
答案 1 :(得分:5)
在Swift中可用:
var timescore = Int()
var actionwait = SKAction.waitForDuration(0.5)
var timesecond = Int()
var actionrun = SKAction.runBlock({
timescore++
timesecond++
if timesecond == 60 {timesecond = 0}
scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)"
})
scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
答案 2 :(得分:5)
我已经采取了上面的快速示例,并为时钟添加了前导零。
func updateClock() {
var leadingZero = ""
var leadingZeroMin = ""
var timeMin = Int()
var actionwait = SKAction.waitForDuration(1.0)
var timesecond = Int()
var actionrun = SKAction.runBlock({
timeMin++
timesecond++
if timesecond == 60 {timesecond = 0}
if timeMin / 60 <= 9 { leadingZeroMin = "0" } else { leadingZeroMin = "" }
if timesecond <= 9 { leadingZero = "0" } else { leadingZero = "" }
self.flyTimeText.text = "Flight Time [ \(leadingZeroMin)\(timeMin/60) : \(leadingZero)\(timesecond) ]"
})
self.flyTimeText.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
}
答案 3 :(得分:1)
这是使用Xcode 9.3和Swift 4.1为SpriteKit构建计时器的完整代码
在我们的示例中,分数标签将每2秒递增1。 这是最终结果
好,我们开始吧!
首先,我们需要一个标签
class GameScene: SKScene {
private let label = SKLabelNode(text: "Score: 0")
}
class GameScene: SKScene {
private let label = SKLabelNode(text: "Score: 0")
override func didMove(to view: SKView) {
self.label.fontSize = 60
self.addChild(label)
}
}
现在标签位于屏幕的中心。让我们运行项目来查看它。
请注意,此时标签尚未更新!
我们还想构建一个计数器属性,它将保存标签显示的当前值。我们还希望一旦计数器属性发生变化就更新标签......
class GameScene: SKScene {
private let label = SKLabelNode(text: "Score: 0")
private var counter = 0 {
didSet {
self.label.text = "Score: \(self.counter)"
}
}
override func didMove(to view: SKView) {
self.label.fontSize = 60
self.addChild(label)
// let's test it!
self.counter = 123
}
}
最后,我们想要建立一个每2秒增加计数器
的动作class GameScene: SKScene {
private let label = SKLabelNode(text: "Score: 0")
private var counter = 0 {
didSet {
self.label.text = "Score: \(self.counter)"
}
}
override func didMove(to view: SKView) {
self.label.fontSize = 60
self.addChild(label)
// 1 wait action
let wait2Seconds = SKAction.wait(forDuration: 2)
// 2 increment action
let incrementCounter = SKAction.run { [weak self] in
self?.counter += 1
}
// 3. wait + increment
let sequence = SKAction.sequence([wait2Seconds, incrementCounter])
// 4. (wait + increment) forever
let repeatForever = SKAction.repeatForever(sequence)
// run it!
self.run(repeatForever)
}
}
答案 4 :(得分:-3)
以下代码创建一个新线程,并在主线程上执行某些操作之前等待2秒:
BOOL continueIncrementingScore = YES;
dispatch_async(dispatch_queue_create("timer", NULL);, ^{
while(continueIncrementingScore) {
[NSThread sleepForTimeInterval:2];
dispatch_async(dispatch_get_main_queue(), ^{
// this is performed on the main thread - increment score here
});
}
});
每当您想要停止它时 - 只需将continueIncrementingScore
设置为NO