ios - touchesBegan on local variable -
i have function gets called repeatedly every 2 seconds, bringing ball top of screen random texture each time. want able work ball in touchesbegan can't because local variable. i've tried making global variable gave me error saying tried add skspritenode has parent. appreciated. here's code used bring down balls.
override func didmovetoview(view: skview) { var create = skaction.runblock({() in self.createtargets()}) var wait = skaction.waitforduration(2) var waitandcreateforever = skaction.repeatactionforever(skaction.sequence([create, wait])) self.runaction(waitandcreateforever) } func createtargets() { let randomx = int(arc4random_uniform(170) + 290) var ball = skspritenode(imagenamed: "blueblue") ball.zposition = 0 ball.physicsbody = skphysicsbody(circleofradius: ball.size.width / 11) ball.physicsbody?.dynamic = true ball.size = cgsize(width: ball.size.width / 1.5, height: ball.size.height / 1.5) let random = int(arc4random_uniform(35)) let textures = [texture1, texture2, texture3, texture4, texture5, texture6, texture7, texture8, texture9, texture10, texture11, texture12, texture13, texture14, texture15, texture16, texture17, texture18, texture19, texture20, texture21, texture22, texture23, texture24, texture25, texture1, texture7, texture18, texture24, texture25, texture1, texture7, texture18, texture24, texture25] ball.texture = textures[random] ball.position = cgpoint(x: randomx, y: 1400) addchild(ball) }
local variable (in function) little. (the variable gets destroyed exit createtargets
.
a global variable presumably much. apparently, trying create upfront.
--> use class instance variable.
besides, in touchesbegan
can check node being touched , - depending on intend - might able without variable.
Comments
Post a Comment