ios - make my enemy spawn a little faster when reach a certain score -
func updatetimer() { if score < 10 { runaction(skaction.repeatactionforever(skaction.sequence([skaction.runblock(addenemy), skaction.waitforduration(1.0)]))) }else if score == 10 { runaction(skaction.repeatactionforever(skaction.sequence([skaction.runblock(addenemy), skaction.waitforduration(0.5)]))) }else if score == 20 { runaction(skaction.repeatactionforever(skaction.sequence([skaction.runblock(addenemy), skaction.waitforduration(0.1)]))) } }
i trying make spawn faster when player reach score code has error can me please thank whoever answers question
i wanted make 1
func updatetimer() { if score < 3 { timerdots = nstimer.scheduledtimerwithtimeinterval(1.5, target: self, selector: "firedot", userinfo: nil, repeats: true) // dots generated bit faster before after score reaches 3 } else if score == 3 { timerdots?.invalidate() timerdots = nstimer.scheduledtimerwithtimeinterval(1.0, target: self, selector: "firedot", userinfo: nil, repeats: true) // dots generated faster before after score reaches 12 } else if score == 12 { timerdots?.invalidate() timerdots = nstimer.scheduledtimerwithtimeinterval(0.9, target: self, selector: "firedot", userinfo: nil, repeats: true) } }
it's simpler using instance variable , skscene
update:
method:
private var score: int = 0 private var priorenemyaddtime: cftimeinterval = 0 private var nextenemyaddtime: cftimeinterval { var waittime = score < 10 ? 1.0 : score < 20 ? 0.5 : 0.1 return priorenemyaddtime + waittime } override func update(currenttime: cftimeinterval) { if nextenemyaddtime <= currenttime { priorenemyaddtime = currenttime addenemy() } }
on every frame, check whether it's been long enough since last time added enemy. if so, save current time time of last add, , add enemy.
Comments
Post a Comment