physics - Why does SKLabelNode increment too much on Contact? (Swift) -
i trying have label increment +1 every time sprite makes contact contact node, increments large numbers +23. think happening taking account every millisecond sprite touching node, don't know how fix it. here label node code within didmovetoview:
score = 0 scorelabelnode = sklabelnode(fontnamed:"[z] arista light") scorelabelnode.position = cgpointmake(self.frame.size.width/2, self.frame.size.height/4) scorelabelnode.zposition = 100 scorelabelnode.fontsize = 500 scorelabelnode.alpha = 0.03 scorelabelnode.text = string(score) self.addchild(scorelabelnode) here contact node:
var contactnode = sknode() contactnode.position = cgpoint(x: self.frame.size.width + asteroidtexture.size().height / 15 + rocket.size.width, y: 0.0) contactnode.physicsbody = skphysicsbody(rectangleofsize: cgsizemake( asteroid.size.width/16, self.frame.size.height*2 )) contactnode.physicsbody?.dynamic = false contactnode.physicsbody?.categorybitmask = scorecategory contactnode.physicsbody?.contacttestbitmask = rocketcategory contactnode.runaction(asteroidsmoveandremove) moving.addchild(contactnode) and here code when rocket sprite makes contact contactnode increments:
func didbegincontact(contact: skphysicscontact) { if moving.speed > 0 { if ( contact.bodya.categorybitmask & scorecategory ) == scorecategory || ( contact.bodyb.categorybitmask & scorecategory ) == scorecategory { // rocket has contact score entity score++ scorelabelnode.text = string(score) println("hit") } else{ gameover() } } 'moving' when asteroid sprites moving, contact nodes move it
it's not every millisecond, instead every frame, , that's why end numbers +23 (the collision taking place 1/3 of second , if it's running @ 60 fps 20 or frames).
one thing subclass sknode actual contactnode class has property hasbeenstruck set false or extent.
then when check contact, first check see if hasbeenstruck false. if is, register hit , update score , set contactnode's hasbeenstruck property true. way, on next 20 or checks see if contact has happened, won't keep updating label because if condition has failed.
however, if game allows user hit same contactnode twice , score both times, isn't complete solution. if case, use timer or "invincibility" period given contactnode.
Comments
Post a Comment