ios - Insert counter enemies killed -
i'm developing simple 2d play , implement counter every enemy killed , keep displayed on display until game over.
how this? i'm using xcode 7.3.1
my enemies code :
func frecciaincollisioneconnemico(freccia:skspritenode, nemico:skspritenode) { print("freccia ha colpito un nemico") freccia.removefromparent() nemico.removefromparent() nemicidistrutti += 1 print("hai distrutto \(nemicidistrutti) nemici") if (nemicidistrutti >= 20) { let rivela = sktransition.fliphorizontalwithduration(0.5) let gameoverscene = gameoverscene(size: self.size, vinto: true) self.view?.presentscene(gameoverscene, transition: rivela) } }
you should able answer question easy.
create label
class gamescene: skscene { let enemieskilledlabel = sklabelnode(fontnamed: "helveticaneue") override func didmovetoview(view: skview) { loadenemieskilledlabel() } private func loadenemieskilledlabel() { enemieskilledlabel.position = ... enemieskilledlabel.text = "0" ... addchild(enemieskilledlabel) } }
than in death function update text.
... nemicidistrutti += 1 enemieskilledlabel.text = "\(nemicidistrutti)" // update text
this called string interpolation, can read more here
as tip should change collision method take in optionals. there case 1 collision calls multiple contacts because multiple body parts collided. code not taking account , therefore crash if frecciaincollisioneconnemico called multiple times in fast succession.
change this
func frecciaincollisioneconnemico(freccia: skspritenode?, nemico: skspritenode?) { guard let freccia = freccia, nemico = nemico else { return } freccia.removefromparent() nemico.removefromparent() ... }
lastly recommend try write code in english only.
hope helps
Comments
Post a Comment