swift - Multiple circles drawn SpriteKit -
i'm working on simple game based on paiting background player. in left , right corner there buttons move character left or right. i've implemented (character moving , lefts painted background behind), adding circles fps's drops fast. there solution that?
import spritekit class gamescene: skscene { var playerdot:playerdot = playerdot(imagenamed:"player") var isturningleft:bool = false var isturningright:bool = false var lastlocation:cgpoint = cgpoint() override func didmovetoview(view: skview) { /* setup scene here */ let mylabel = sklabelnode(fontnamed:"helvetica") mylabel.name = "left" mylabel.text = "left" mylabel.fontsize = 30 mylabel.horizontalalignmentmode = .left mylabel.position = cgpoint(x:cgrectgetminx(self.frame), y:cgrectgetminy(self.frame)) self.addchild(mylabel) let mylabel2 = sklabelnode(fontnamed:"helvetica") mylabel2.name = "right" mylabel2.text = "right" mylabel2.fontsize = 30 mylabel2.horizontalalignmentmode = .right mylabel2.position = cgpoint(x:cgrectgetmaxx(self.frame), y:cgrectgetminy(self.frame)) self.addchild(mylabel2) playerdot.position = cgpoint(x:cgrectgetmaxx(self.frame)/2, y:cgrectgetminy(self.frame)) self.addchild(playerdot) } override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { touch in touches { let location = touch.locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "left" { isturningleft = true isturningright = false } else if thename == "right" { isturningright = true isturningleft = false } } } } override func touchesended(touches: set<uitouch>, withevent event: uievent?) { touch in touches { let location = touch.locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "left" { isturningleft = false } else if thename == "right" { isturningright = false } } } } override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) { touch in touches { let location = touch.locationinnode(self) if let thename = self.nodeatpoint(location).name { if thename == "left" { isturningleft = false } else if thename == "right" { isturningright = false } } } } override func update(currenttime: cftimeinterval) { if(isturningleft){ playerdot.increaseangle() } else if (isturningright){ playerdot.decreaseangle() } //calculates new character position based on angle of movement changed playerdot.updateposition() drawcircle() } func drawcircle(){ if(distancefromcgpoints(lastlocation, b: playerdot.position)>2){ let circle = skshapenode(circleofradius: 10 ) circle.position = playerdot.position circle.fillcolor = skcolor.orangecolor() circle.strokecolor = skcolor.orangecolor() self.addchild(circle) lastlocation = playerdot.position } } func distancefromcgpoints(a:cgpoint,b:cgpoint)->cgfloat{ return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); } func random() -> cgfloat { return cgfloat(float(arc4random()) / 0xffffffff) } func random(min min: cgfloat, max: cgfloat) -> cgfloat { return random() * (max - min) + min } }
edit: drawcircle skshapenode replaced skspritenote
func drawcircle(){ if(distancefromcgpoints(lastlocation, b: playerdot.position)>2){ let playerdot2 = skspritenode(imagenamed:"player") playerdot2.position = playerdot.position self.addchild(playerdot2) lastlocation = playerdot.position } }
if having frame rate drops parts of code slowing down execution much. need optimise code. use instruments (time profiler) find out lines/functions causing problems speed wise. if have never used before read on , use couple times gist, recommend watching wwdc video profiling in-depth
Comments
Post a Comment