can me, im trying make simple app touch screen , 4 images randomly displayed. problem is, when tap screen don't image have no errors. help!
here's code:
import spritekit class gamescene: skscene { var num1 = skspritenode(imagenamed: "num1") var num2 = skspritenode(imagenamed: "num2") var num3 = skspritenode(imagenamed: "num3") var num4 = skspritenode(imagenamed: "num4") override func didmovetoview(view: skview) { self.physicsworld.gravity = cgvectormake(0, -9.8) num1.size = cgsize(width: 150, height: 150) num2.size = cgsize(width: 150, height: 150) num3.size = cgsize(width: 150, height: 150) num4.size = cgsize(width: 150, height: 150) let scenebody = skphysicsbody(edgeloopfromrect: self.frame) scenebody.friction = 0 self.physicsbody = scenebody } override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { touch: anyobject in touches { _ = touch.locationinnode(self) let random = arc4random_uniform(4) switch random { case 0: self.addchild(num1) print("number 0") case 1: self.addchild(num2) print("number 1") case 2: self.addchild(num3) print("number 2") default: self.addchild(num4) print("default") } } } }
you not setting position each sprite should appear
before each self.addchild(...)
should exec this
numx.position = location
and rearrange touchesbegan method this
override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { var location touch in touches { location = touch.locationinnode(self) } let random = arc4random_uniform(4) switch random { case 0: num1.position = location self.addchild(num1) print("number 0") case 1: num2.position = location self.addchild(num2) print("number 1") case 2: num3.position = location self.addchild(num3) print("number 2") default: num4.position = location self.addchild(num4) print("default") } }
Comments
Post a Comment