ios - Swift - How to change background color when a certain amount of points are gained -
i have been working on game when player gets 10 points background change color put in string, same happen 20 points, 30 points , on. question how can make background fade different colors when player gets on 10 points /20 points /30 points. don't want colors random want put own color codes/hex values, don't want colors change when button pressed. want change when player gets on amount of points.
a example of game "don't touch spikes" every 5 points gain background fades different one.
note: made game in gamescene , not using gameviewcontroller made project game file so:
import foundation import spritekit class gamescene: skscene, skphysicscontactdelegate { var movingground: ppmovingground! var hero: pphero! var wallgen: ppwallgen! var isstarted = false var isgameover = false override func didmovetoview(view: skview) { //backgroundcolor = uicolor.greencolor() backgroundcolor = uicolor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0) addmovingground() addhero() addwallgen() addtaptostartlabel() addstagelabel() addpointslabels() addphysicsworld() loadhighscore() } func addlevellabel() { } func addmovingground() { movingground = ppmovingground(size: cgsizemake(view!.frame.width, kmlgroundheight)) movingground.position = cgpointmake(0, view!.frame.size.height/2) addchild(movingground) } func addhero() { hero = pphero() hero.position = cgpointmake(70, movingground.position.y + movingground.frame.size.height/2 + hero.frame.size.height/2) addchild(hero) } func addwallgen() { wallgen = ppwallgen(color: uicolor.clearcolor(), size: view!.frame.size) wallgen.position = view!.center addchild(wallgen) } func addtaptostartlabel() { let taptostartlabel = sklabelnode(text: "tap start!") taptostartlabel.name = "taptostartlabel" taptostartlabel.position.x = view!.center.x taptostartlabel.position.y = view!.center.y + 40 taptostartlabel.fontname = "helvetica" taptostartlabel.fontcolor = uicolor.whitecolor() taptostartlabel.fontsize = 22.0 addchild(taptostartlabel) taptostartlabel.runaction(blinkanimation()) } func addstagelabel() { let stagelabel = ppstagelabel(num: 1) stagelabel.name = "stagelabel" stagelabel.position.x = view!.center.x stagelabel.position.y = view!.center.y - 120 stagelabel.fontcolor = uicolor.whitecolor() stagelabel.fontname = "helvetica" stagelabel.fontsize = 40 addchild(stagelabel) let stagetextlabel = sklabelnode(text: "stage") stagetextlabel.fontcolor = uicolor.whitecolor() stagetextlabel.fontsize = 14.0 stagetextlabel.fontname = "helvetica" stagetextlabel.position = cgpointmake(3.0,-15.0) stagelabel.addchild(stagetextlabel) } func addpointslabels() { let pointslabel = pppointslabel(num: 0) pointslabel.name = "pointslabel" pointslabel.position.x = view!.center.x pointslabel.position.y = view!.center.y + 120 pointslabel.fontcolor = uicolor.whitecolor() pointslabel.fontname = "helvetica" pointslabel.fontsize = 40 addchild(pointslabel) let highscorelabel = pppointslabel(num: 0) highscorelabel.name = "highscorelabel" highscorelabel.position = cgpointmake(view!.frame.size.width - 40, view!.frame.size.height - 30) highscorelabel.fontcolor = uicolor.whitecolor() highscorelabel.fontname = "helvetica" highscorelabel.fontsize = 24 addchild(highscorelabel) let highscoretextlabel = sklabelnode(text: "highscore: ") highscoretextlabel.fontcolor = uicolor.whitecolor() highscoretextlabel.fontsize = 14.0 highscoretextlabel.fontname = "helvetica" highscoretextlabel.position = cgpointmake(-70.0,3.5) highscorelabel.addchild(highscoretextlabel) } func addphysicsworld() { physicsworld.contactdelegate = self } func loadhighscore() { let defaults = nsuserdefaults.standarduserdefaults() let highscorelabel = childnodewithname("highscorelabel") as! pppointslabel highscorelabel.setto(defaults.integerforkey("highscore")) } // mark - game lifecycle func start() { isstarted = true let taptostartlabel = childnodewithname("taptostartlabel") taptostartlabel?.removefromparent() hero.stop() movingground.start() wallgen.startgenwallsevery(1) } func gameover() { isgameover = true // stops hero.fall() wallgen.stopwalls() movingground.stop() hero.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) gameoverlabel.runaction(blinkanimation()) // save current points label value let pointslabel = childnodewithname("pointslabel") as! pppointslabel let highscorelabel = childnodewithname("highscorelabel") as! pppointslabel let stagelabel = childnodewithname("stagelabel") as! ppstagelabel if highscorelabel.number < pointslabel.number { highscorelabel.setto(pointslabel.number) let defaults = nsuserdefaults.standarduserdefaults() defaults.setinteger(highscorelabel.number, forkey: "highscore") } } func restart() { let newscence = gamescene(size: view!.bounds.size) newscence.scalemode = .aspectfill view!.presentscene(newscence) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { if isgameover { restart() }else if !isstarted { start() }else{ hero.flip() } } override func update(currenttime: cftimeinterval) { if wallgen.walltrackers.count > 0 { let wall = wallgen.walltrackers[0] ppwall let walllocation = wallgen.convertpoint(wall.position, tonode: self) if walllocation.x < hero.position.x { wallgen.walltrackers.removeatindex(0) let pointslabel = childnodewithname("pointslabel") as! pppointslabel pointslabel.increment() } }else if wallgen.walltrackers.count > 0 { let wall = wallgen.walltrackers[0] ppwall let walllocation = wallgen.convertpoint(wall.position, tonode: self) if walllocation.x < hero.position.x { wallgen.walltrackers.removeatindex(0) let stagelabel = childnodewithname("stagelabel") as! ppstagelabel stagelabel.increment() } } } // mark: - skphysicscontactdelegate func didbegincontact(contact: skphysicscontact) { if !isgameover { gameover() } } // marr: - animations func blinkanimation() -> skaction { let duration = 0.4 let fadeout = skaction.fadealphato(0.0, duration: duration) let fadein = skaction.fadealphato(1.0, duration: duration) let blink = skaction.sequence([fadeout, fadein]) return skaction.repeatactionforever(blink) } }
i think can't animate skview.first declare variables
var isstarted = false var isgameover = false var lastchangedbackground = 0 var background = skspritenode()
to want add sknode child of skscene , let background , create variable lastchangedbackground in didmovetoview() function. lastchangedbackground let background change color once when value of points.
then can add update function this:
override func didmovetoview(view: skview) { //backgroundcolor = uicolor.greencolor() //backgroundcolor = skspritenode() var background = skspritenode(color: uicolor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0),size: view.bounds.size) background.position = view.center self.addchild(background) addmovingground() addhero() addwallgen() addtaptostartlabel() addstagelabel() addpointslabels() addphysicsworld() loadhighscore() } override func update(currenttime: cftimeinterval) { if wallgen.walltrackers.count > 0 { let wall = wallgen.walltrackers[0] ppwall let walllocation = wallgen.convertpoint(wall.position, tonode: self) if walllocation.x < hero.position.x { wallgen.walltrackers.removeatindex(0) let pointslabel = childnodewithname("pointslabel") as! pppointslabel pointslabel.increment() //here points should receive pointslabel value //if pointslabel.num % 10 == 0 points 10, 20, 30... if pointslabel.num % 10 == 0 && pointslabel.num != lastchangedbackground{ lastchangedbackground = points if (lastchangedbackground == 10) { background.runaction(skaction.colorizewithcolor(skcolor.bluecolor(), colorblendfactor: 0.5, duration: 1)) } else if(lastchangedbackground == 20){ background.runaction(skaction.colorizewithcolor(skcolor.greencolor(), colorblendfactor: 0.5, duration: 1)) } else if(lastchangedbackground == 30){ background.runaction(skaction.colorizewithcolor(skcolor.redcolor(), colorblendfactor: 0.5, duration: 1)) } wall in wallgen.walltrackers { wall.runaction(skaction.colorizewithcolor(background.color, colorblendfactor: 0.5, duration: 1)) } } } }else if wallgen.walltrackers.count > 0 { let wall = wallgen.walltrackers[0] ppwall let walllocation = wallgen.convertpoint(wall.position, tonode: self) if walllocation.x < hero.position.x { wallgen.walltrackers.removeatindex(0) let stagelabel = childnodewithname("stagelabel") as! ppstagelabel stagelabel.increment() } } }
Comments
Post a Comment