lua - Corona Storyboard, declaring an object then making it moveable, nil value attempt -
i creating game, , in level 1 want load few images represent letters, , want add functionalities them. 1 of them ability move them.
so inside enterscene is
function scene:enterscene(event) ... leta = display.newimage("media/letters/a.png", display.contentwidth/4 - 20, display.contentheight/5 - 18) letc = display.newimage("media/letters/c.png", display.contentwidth/4 + 35, display.contentheight/5 - 18) letr= display.newimage("media/letters/r.png", display.contentwidth/4 + 90, display.contentheight/5 - 18) lete=display.newimage("media/letters/e.png", display.contentwidth/4 + 145, display.contentheight/5 - 18) screengroup:insert(leta) screengroup:insert(letc) screengroup:insert(letr) screengroup:insert(lete) leta:addeventlistener("touch", leta) letc:addeventlistener("touch", letc) letr:addeventlistener("touch", letr) lete:addeventlistener("touch", lete)
now added moving function leta, is
function leta:touch(event) if event.phase=="began" display.getcurrentstage():setfocus(self, event.id) self.isfocus = true self.markx = self.x self.marky = self.y elseif self.isfocus if event.phase=="moved" self.x = event.x - event.xstart + self.markx self.y = event.y - event.ystart + self.marky elseif event.phase=="ended" or event.phase == "cancelled" display.getcurrentstage():setfocus(self,nil) self.isfocus = false end end return true end
then when try scene gives me error line of function leta:touch(event), says "attempt index global 'leta'.
what do then? designed because want when user clicks play, load letters, , if presses back, unload them screen.
you can instead, declare touch function via ":" (colon) leta or other objects not tables.
local leta = display.newimage("media/letters/a.png", display.contentwidth/4 - 20, display.contentheight/5 - 18) leta.touch = function(self,event) if event.phase=="began" display.getcurrentstage():setfocus(self, event.id) self.isfocus = true self.markx = self.x self.marky = self.y elseif self.isfocus if event.phase=="moved" self.x = event.x - event.xstart + self.markx self.y = event.y - event.ystart + self.marky elseif event.phase=="ended" or event.phase == "cancelled" display.getcurrentstage():setfocus(self,nil) self.isfocus = false end end return true end leta:addeventlistener("touch")
Comments
Post a Comment