This the the class code:
-------------------- -- REQUIRE -------------------- -- STORYBOARD local sb = require "storyboard" -------------------- -- ENUM -------------------- local state = { idle = "idle", move = "move", eat = "eat", play = "play", sleep= "sleep" } -------------------- -- FISH CLASS -------------------- local fish = {} local fish\_mt = { \_\_index = fish } -- CONSTRUCTOR function fish.new( fishData ) -- Create the instance of the fish (sprite) local fishSheet = graphics.newImageSheet( fishData.sheetPath, fishData.sheetSetting ) local instance = display.newSprite( fishSheet, fishData.spriteSequence ) instance.state = state.idle -- Cant play animation here will crash randomly as this is a local copy instance.x = display.contentWidth \* 0.5 instance.y = display.contentHeight \* 0.5 return setmetatable( instance, fish\_mt ) end -- MEMBER FUNCTIONS function fish:playAnim() self:setSequence( self.state ) self:play() end function fish:changeState( newState ) if ( self.state ~= newState ) then self.state = newState -- NYI end end -- RETURN return fish
My problem:
-
I can’t call setSequence or play outside or inside the class. Example:
local obj = require “objclass” local function game() --local fishData = {Some data here} local fish = obj.new( fishData ) – fish:setSequence( “idle” ) – fish:play() – OR – fish:playAnim() end
Either uncommenting line 5 and 6, or uncommenting 8 will crash. Is there a way I can play animation of a sprite created inside a class? Any help would be appreciated.