iphone - Swift/SpriteKit - How to add points when an object collides with a player -
i want know how give player plus 1 point every time player collides object. every time object collided with, @ on previous amount. example: if have 5 coins , collected 5 in current game overall amount of coins have 10. grate if link me place has tutorial of in swift not in objective-c.
note: have set-up coins spawning , collision works when player collides game ends.
code, have removed code not necessary:
import foundation import spritekit class gamescene: skscene, skphysicscontactdelegate { var movingground: ppmovingground! var square1: ppsquare1! var wallgen: ppwallgen! var diamondgen: ppdiamondgen! var isstarted = false var screentapped = avaudioplayer() var isgameover = false var isdiamondcontact = false override func didmovetoview(view: skview) { addmovingground() addsquare1() adddiamondgen() addwallgen() start() adddiamondslabels() } func addmovingground() { movingground = ppmovingground(size: cgsizemake(view!.frame.width, kmlgroundheight)) movingground.position = cgpointmake(0, view!.frame.size.height/2) addchild(movingground) } func addsquare1() { square1 = ppsquare1() square1.position = cgpointmake(70, movingground.position.y + movingground.frame.size.height/2 + square1.frame.size.height/2) square1.zposition = 1 playernode.addchild(square1) } func adddiamondgen() { diamondgen = ppdiamondgen(color: uicolor.clearcolor(), size: view!.frame.size) diamondgen.position = view!.center addchild(diamondgen) } func addwallgen() { wallgen = ppwallgen(color: uicolor.clearcolor(), size: view!.frame.size) wallgen.position = view!.center addchild(wallgen) } func adddiamondslabels() { let diamondslabel = ppdiamondslabel(num: 0) diamondslabel.name = "diamondpointslabel" diamondslabel.alpha = 0.50 diamondslabel.position.x = view!.center.x - 120 diamondslabel.position.y = view!.center.y + 1000 diamondslabel.fontcolor = uicolor.whitecolor() diamondslabel.fontname = "helvetica" diamondslabel.fontsize = 40 addchild(diamondslabel) let diamondtotallabel = ppdiamondslabel(num: 0) diamondtotallabel.name = "diamondhighscorelabel" diamondtotallabel.alpha = 0.50 diamondtotallabel.position = cgpointmake(view!.frame.size.width - 40, view!.frame.size.height - 60) diamondtotallabel.fontcolor = uicolor.whitecolor() diamondtotallabel.fontname = "helvetica" diamondtotallabel.fontsize = 24 addchild(diamondtotallabel) let diamondtotaltextlabel = sklabelnode(text: "diamonds: ") diamondtotaltextlabel.alpha = 0.95 diamondtotaltextlabel.fontcolor = uicolor.whitecolor() diamondtotaltextlabel.fontsize = 22.0 diamondtotaltextlabel.fontname = "helvetica" diamondtotaltextlabel.position = cgpointmake(-90.0,2.0) diamondtotallabel.addchild(diamondtotaltextlabel) } func start() { isstarted = true square1.stop() movingground.start() wallgen.startgenwallsevery(1) diamondgen.startgendiamondsevery(1) } func collisionwithdiamond() { isdiamondcontact = true } // mark - game lifecycle func gameover() { isgameover = true // stops square1.fall() wallgen.stopwalls() diamondgen.stopdiamonds() movingground.stop() square1.stop() // create game on label let gameoverlabel = sklabelnode(text: "game over!") gameoverlabel.fontcolor = uicolor.whitecolor() gameoverlabel.fontname = "helvetica" gameoverlabel.position.x = view!.center.x gameoverlabel.position.y = view!.center.y + 80 gameoverlabel.fontsize = 22.0 addchild(gameoverlabel) override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { } override func update(currenttime: cftimeinterval) { } // mark: - skphysicscontactdelegate func didbegincontact(contact: skphysicscontact) { if !isgameover { gameover() } else { !isdiamondcontact collisionwithdiamond() } }
you can create skspritenodes (that collide) .physicsbody.categorybitmask = whateverbitmask , in didbegincontact can check see if either contact.bodya.categorybitmask == whateverbitmask or contact.bodyb.categorybitmask == whateverbitmask. if true increment coin total. of course need implement whateverbitmask (see below)
have @ section on collisions @ http://www.raywenderlich.com/87231/make-game-like-mega-jump-sprite-kit-swift-part-1 further information
Comments
Post a Comment