use*_*149 29 timer objective-c ios sprite-kit
我怎样才能创建一个每两秒触发一次的计时器,它会在我屏幕上的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];
}
}
Run Code Online (Sandbox Code Playgroud)
Lea*_*s2D 60
在Sprite Kit中不使用 NSTimer
,performSelector:afterDelay:
或Grand Central Dispatch(GCD,即任何dispatch_...
方法),因为这些计时方法忽略节点,场景或视图的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]]];
Run Code Online (Sandbox Code Playgroud)
并重复运行代码:
[node runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]]];
Run Code Online (Sandbox Code Playgroud)
或者performSelector:onTarget:
,如果您需要模仿SKScene 方法并且不知道如何将其转发到节点或转发不方便的地方,您也可以使用代替runBlock:
或可能使用.customActionWithDuration:actionBlock:
update:
有关详细信息,请参阅SKAction参考.
更新:使用Swift的代码示例
斯威夫特3
run(SKAction.repeatForever(SKAction.sequence([
SKAction.run( /*code block or a func name to call*/ ),
SKAction.wait(forDuration: 2.5)
])))
Run Code Online (Sandbox Code Playgroud)
斯威夫特2
let wait = SKAction.wait(forDuration:2.5)
let action = SKAction.run {
// your code here ...
}
run(SKAction.sequence([wait,action]))
Run Code Online (Sandbox Code Playgroud)
Aund重复运行代码:
let wait = SKAction.waitForDuration(2.5)
let run = SKAction.runBlock {
// your code here ...
}
runAction(SKAction.sequence([wait, run]))
Run Code Online (Sandbox Code Playgroud)
小智 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])))
Run Code Online (Sandbox Code Playgroud)
我已经采取了上面的快速示例并添加了时钟的前导零.
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])))
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14478 次 |
最近记录: |