lua - Attempt to index local 'star1' (a nil value) -- I've researched everywhere and don't get it -
i've been stuck on while , need help. i'm trying have these objects spawn on screen , allow player drag them around want.
at moment random objects flow onto screen, when try click on 'star1' error :
attempt index local 'star1' (a nil value) how should properly?
here code:
display.setstatusbar(display.hiddenstatusbar) local physics = require('physics') physics.start() --physics.setgravity(0, 0) _w = display.contentwidth; --returns screen width _h = display.contentheight; --returns screen height local bg = display.newimage('bg1.png') local startable = {} ship = display.newimage("head.png") ship.name = "ship" ship.x = -80 ship.y = 100 physics.addbody(ship, { issensor = true }) ship.bodytype = 'dynamic' shipintro = transition.to(ship,{time=4000, x=200}) ship.width = 40 ship.height = 40 ship.gravityscale = 0 local star1 = display.newimage("acorn") star1.name = "star1" physics.addbody(star1, { issensor = true }) star1.bodytype = 'static' function initstar() local star1 = {} star1.imgpath = "acorn.png" star1.movementspeed = 10000 table.insert(startable, star1) local star2 = {} star2.imgpath = "enemya.png" star2.movementspeed = 12000 table.insert(startable, star2); local star3 = {} star3.imgpath = "star3.png" star3.movementspeed = 14000 table.insert(startable, star3) end function getrandomstar() local temp = startable[math.random(1, #startable)] local randomstar = display.newimage(temp.imgpath) randomstar.myname = "star" randomstar.movementspeed = temp.movementspeed randomstar.x = math.random(0,_w) randomstar.y = _h + 50 -- start star off screen randomstar.rotation = math.random(0,360) starmove = transition.to(randomstar, { time=randomstar.movementspeed, y=-45, oncomplete = function(self) self.parent:remove(self); self = nil; end }) -- move star end function star1:touch( event ) if event.phase == "began" self.markx = self.x self.marky = self.y elseif event.phase == "moved" local x = (event.x - event.xstart) + self.markx local y = (event.y - event.ystart) + self.marky self.x, self.y = x, y -- move object based on calculations above end return true end star1:addeventlistener( "touch", star1 ) startimer1 = timer.performwithdelay(1700,getrandomstar, 0) startimer2 = timer.performwithdelay(2300,getrandomstar, 0) startimer3 = timer.performwithdelay(2700,getrandomstar, 0) initstar()
instead of code:
local star1 = display.newimage("acorn") -- think line no: 27
try following:
local star1 = display.newimage("acorn.png") --[[ add image name suffix/extension --]]
keep coding............. :)
Comments
Post a Comment