swift images stacking / duplicating - iOS -
i've started coding , first attempt ios game, part of game i'm working on has following elements.
scrolling foreground , background cat jumps when clicked missile raining down above.
i've managed spawn missile every few seconds in doing think i've messed something.
now have 2 errors, first cat, foreground , background images duplicating , stacking on screen , other build error under override func update(currenttime
. i've commented out cat's z rotation otherwise game wont build.
if can me work out how fix i'd grateful.
sorry if it's overkill, here code....
import spritekit class gamescene: skscene, skphysicscontactdelegate { var cat = skspritenode() var crow = skspritenode() var crowtexture1 = sktexture() var skycolor = skcolor() var moveandremovecrow = skaction() var spawn = skaction() var lastmissileadded : nstimeinterval = 0.0 let missilevelocity : cgfloat = 5.0 override func didmovetoview(view: skview) { self.addmissile() } func addmissile() { // initializing missile node var missile = skspritenode(imagenamed: "red-missile") missile.setscale(0.15) // adding spritekit physics body collision detection missile.physicsbody = skphysicsbody(rectangleofsize: missile.size) // missile.physicsbody?.categorybitmask = uint32(obstaclecategory) missile.physicsbody?.dynamic = true // missile.physicsbody?.contacttestbitmask = uint32(shipcategory) missile.physicsbody?.collisionbitmask = 0 missile.physicsbody?.usesprecisecollisiondetection = true missile.name = "missile" // selecting random y position missile var random : cgfloat = cgfloat(arc4random_uniform(300)) missile.position = cgpoint(x: self.frame.size.width / 1.8, y: self.frame.size.height / 1.2 ) self.addchild(missile) } func moveobstacle() { self.enumeratechildnodeswithname("missile", usingblock: { (node, stop) -> void in if let obstacle = node as? skspritenode { obstacle.position = cgpoint(x: obstacle.position.x - self.missilevelocity, y: obstacle.position.y) if obstacle.position.x < 0 { obstacle.removefromparent() } } }) skycolor = skcolor(red:113.0/255.0, green:197.0/255.0, blue:207.0/255.0, alpha:1.0) self.backgroundcolor = skycolor var cattexture1 = sktexture(imagenamed: "cat1") cattexture1.filteringmode = sktexturefilteringmode.nearest var cattexture2 = sktexture(imagenamed: "cat2") cattexture2.filteringmode = sktexturefilteringmode.nearest var anim = skaction.animatewithtextures([cattexture1, cattexture2], timeperframe: 0.2) var run = skaction.repeatactionforever(anim) cat = skspritenode(texture: cattexture1) cat.position = cgpoint(x: self.frame.size.width / 2.2, y: self.frame.size.height / 7.0 ) cat.runaction(run) cat.physicsbody = skphysicsbody(circleofradius: cat.size.height / 2.0) cat.physicsbody!.dynamic = true cat.physicsbody!.allowsrotation = false self.addchild(cat) var groundtexture = sktexture(imagenamed: "ground") groundtexture.filteringmode = sktexturefilteringmode.nearest var movegroundsprite = skaction.movebyx(-groundtexture.size().width, y: 0, duration: nstimeinterval(0.01 * groundtexture.size().width)) var resetgroundsprite = skaction.movebyx(groundtexture.size().width, y: 0, duration: 0.0) var movegroundspritesforever = skaction.repeatactionforever(skaction.sequence([movegroundsprite,resetgroundsprite])) var i:cgfloat = 0; < 2 + self.frame.size.width / ( groundtexture.size().width); ++i { var sprite = skspritenode(texture: groundtexture) sprite.position = cgpointmake(i * sprite.size.width, sprite.size.height / 2) sprite.runaction(movegroundspritesforever) self.addchild(sprite) } var dummy = sknode() dummy.position = cgpointmake(0, groundtexture.size().height / 2) dummy.physicsbody = skphysicsbody(rectangleofsize: cgsizemake(self.frame.size.width, groundtexture.size().height)) dummy.physicsbody!.dynamic = false self.addchild(dummy) var skylinetexture = sktexture(imagenamed: "skyline") skylinetexture.filteringmode = sktexturefilteringmode.nearest var moveskylinesprite = skaction.movebyx(-skylinetexture.size().width, y: 0, duration: nstimeinterval(0.1 * skylinetexture.size().width)) var resetskylinesprite = skaction.movebyx(skylinetexture.size().width, y: 0, duration: 0.0) var moveskylinespritesforever = skaction.repeatactionforever(skaction.sequence([moveskylinesprite,resetskylinesprite])) var i:cgfloat = 0; < 2 + self.frame.size.width / ( skylinetexture.size().width); ++i { var sprite = skspritenode(texture: skylinetexture) sprite.zposition = -20; sprite.position = cgpointmake(i * sprite.size.width, sprite.size.height / 2 + groundtexture.size().height) sprite.runaction(moveskylinespritesforever) self.addchild(sprite) } } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { /* called when touch begins */ cat.physicsbody!.velocity = cgvectormake(0, 0) cat.physicsbody!.applyimpulse(cgvectormake(0, 15)) } func clamp(min: cgfloat, max: cgfloat, value: cgfloat) -> cgfloat { if( value > max ) { return max; } else if( value < min ) { return min; } else { return value; } } override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ // cat.zrotation = self.clamp( -1, max: 0.5, value: cat.physicsbody!.velocity.dy * ( cat.physicsbody!.velocity.dy < 0 ? 0.003 : 0.001 ) ); if currenttime - self.lastmissileadded > 1 { self.lastmissileadded = currenttime + 1 self.addmissile() } self.moveobstacle() } }
i've figured out,
i had functions in 'didmovetoview'. after moving them outside block code runs smoothly.
Comments
Post a Comment