i creating game random shapes fall down , each shape has number , , number represent number of clicks user has on image , image removed , score increased 1 , have problem when images created , , when run of images work fine , when click on number of times has on , gets removed want , when same image created , when click on it prints out msg have put "the user has not clicked on image " have put in default in switch stattment in touchended method , :
override func touchesended(touches: nsset, withevent event: uievent) { touch: anyobject in touches { let location = touch.locationinnode(self) switch(nodeatpoint(location)){ case self.circle: if circlecount == 1{ circle.removefromparent() println("score") circlecount = 0 } case self.rectangle: if rectanglecount == 2{ println("score") rectangle.removefromparent() rectanglecount = 0 } case self.square: if squarecount == 3{ println("score") square.removefromparent() squarecount = 0 } case self.polygon: if polygoncount == 4{ println("score") polygon.removefromparent() polygoncount = 0 } default: println("user didnt click on image") } } } and thats 1 of functions displays 1 of images:
func createrectangle(a: cgfloat){ rectangle = skspritenode() rectangle = skspritenode(imagenamed: "rectangle") rectangle.size = cgsize(width: 150, height: 150) rectangle.physicsbody = skphysicsbody(rectangleofsize: rectangle.size) rectangle.physicsbody?.dynamic = true rectangle.physicsbody?.allowsrotation = false rectangle.position = cgpoint(x: a, y: self.frame.size.height) self.addchild(rectangle) } and there function generates random number display shapes randomly.
check out updated code:
import uikit import spritekit import darwin var squarecount = 0 var circlecount = 0 var rectanglecount = 0 var polygoncount = 0 var x = 0 class startgame: skscene { var scorelabel = sklabelnode(fontnamed: "cholkduster") let bg = skspritenode(imagenamed: "background") var score = 0 override func didmovetoview(view: skview) { addbackground() addscorelabel() //random number shapes runaction(skaction.repeatactionforever(skaction.sequence([skaction.runblock(initiateshapes), skaction.waitforduration(1.0)]))) self.physicsworld.gravity = cgvectormake(0, -0.5) } func addscorelabel(){ scorelabel.zposition = 0 self.scorelabel.text = "0" self.scorelabel.fontsize = 42 self.scorelabel.position = cgpoint(x: cgrectgetmidx(self.frame) , y: cgrectgetmidy(self.frame)) self.addchild(scorelabel) } func addbackground() { //background image bg.position = cgpoint(x: cgrectgetmidx(self.frame), y:cgrectgetmidy(self.frame)) bg.size.width = self.frame.size.width bg.size.height = self.frame.size.height bg.zposition = -10 self.addchild(bg) } func initiateshapes() { let shapearray = ["circle", "rectangle", "square", "polygon"] x = int (arc4random_uniform(4)) let shape = skspritenode(imagenamed: shapearray[x]) shape.name = shapearray[x] shape.size = cgsize(width: 150, height: 150) let actualx = random(min: shape.size.height/2, max: size.width - shape.size.height/2) shape.position = cgpoint(x: actualx, y: size.height + shape.size.width / 2) shape.zposition = 10 addchild(shape) let actualduration = random(min: cgfloat(2.0), max: cgfloat(4.0)) let actionmove = skaction.moveto(cgpoint(x: actualx, y: -shape.size.width / 2), duration: 5.1) let actionmovedone = skaction.removefromparent() shape.runaction(skaction.sequence([actionmove, actionmovedone])) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { score++ if let touch = touches.first as? uitouch { let touchlocation = touch.locationinnode(self) if nodeatpoint(touchlocation).name == "circle" { nodeatpoint(touchlocation).removefromparent() } else if nodeatpoint(touchlocation).name == "rectangle" { nodeatpoint(touchlocation).removefromparent() } else if nodeatpoint(touchlocation).name == "square" { nodeatpoint(touchlocation).removefromparent() } else if nodeatpoint(touchlocation).name == "polygon"{ nodeatpoint(touchlocation).removefromparent() } else { println("nothing") } scorelabel.text = "\(score)" } } func random() -> cgfloat { return cgfloat(float(arc4random()) / 0xffffffff) } func random(#min: cgfloat, max: cgfloat) -> cgfloat { return random() * (max - min) + min } func randomint(min: int, max:int) -> int { return min + int(arc4random_uniform(uint32(max - min + 1))) } }
Comments
Post a Comment