Error indexing _super . Help

So here’s my class I tried making.  It’s called marble.lua, and I call it in game.lua.  Everything works as it should (marble being spawned, removing self going offscreen, physics are applied, etc) except one thing, when I click on the marble, I get a peculiar error. So here’s the marble class:

 

local physics = require "physics" physics.start() local M = {} function M.new(options) local options = options or {} --1.Brown 2.Orange 3.Blue 4.Red 5.Black 6.Pink 7.Green 8.Yellow 9.Purple local color = options.color or 1 local colorMarble = display.newImage( "assets/images/".. color ..".png") colorMarble.x = options.x or math.random(display.contentWidth\*0.2, display.contentHeight\*0.7) colorMarble.y = options.y or -200 colorMarble.width = options.w or 70 colorMarble.height = options.h or 70 local function marbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = score + 1 --scoreText.text = score end end local function offscreen(self, event) if (self.y == nil) then return end if (self.y \> display.contentHeight + 50) then Runtime:removeEventListener("enterFrame", self) print("just Deleted Marble") self:removeSelf() end end physics.addBody(colorMarble) colorMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", colorMarble) colorMarble:addEventListener("touch", marbleTouched) return colorMarble end return M

and here’s the error im getting which points to the function marbleTouched, specifically the removeEventListener call.

 

13:32:08.844 ERROR: Runtime error 13:32:08.844 ?:0: attempt to index field '\_super' (a nil value) 13:32:08.844 stack traceback: 13:32:08.844 ?: in function 'removeEventListener' 13:32:08.844 C:\Users\Didier\Documents\Corona Projects\ChaosMarbles2\modules\marbles.lua:21: in function \<C:\Users\Didier\Documents\Corona Projects\ChaosMarbles2\modules\marbles.lua:19\> 13:32:08.844 ?: in function \<?:169\>

Please point me in the right direction!

My guess is some of your code is executing twice or causing two removals.  

Let me make a few  suggestions.

  1. Never use ‘removeSelf()’.  Call ‘display.remove( self )’ instead.  SAFER.

  2. Consolidate your clean up in a finalize call

    – Automatically Clean up when removed colorMarble.finalize = function( self ) if( self.enterFrame ) then Runtime:removeEventListener( “enteFrame”, self ) self.enterFrame = nil end end; colorMarble:addEventListener( “finalize” )

  3. Here is the modified ‘enterFrame’ listener.

    – This is better function colorMarble.enterFrame( self ) if (self.y == nil) then return end if (self.y > display.contentHeight + 50) then display.remove( self ) end end; Runtime:addEventListener( “enterFrame”, colorMarble )

  4. Here is the modified touch listener

    function colorMarble.touch( self, event) if (event.phase == “began”) then display.remove( self ) end return false end; colorMarble:addEventListener( “touch” )

Thanks, I made the changes.

So now the original problem was the marbleTouched function (touching the marble onscreen will delete/remove it). So should I be calling colorMarble.finalize() inside that marbleTouched function?

Did you read the docs about the ‘finalize’ event before asking?

Answer: No.  The ‘finalize’ event is thrown for every object when it is deleted.  So, if you have a ‘finalize’ listener, it executes automatically when the object is removed. 

(re-posted from above)

-- Automatically Clean up when removed colorMarble.finalize = function( self ) if( self.enterFrame ) then Runtime:removeEventListener( "enteFrame", self ) self.enterFrame = nil end end; colorMarble:addEventListener( "finalize" )

You DO NOT call it manually.

 I’m not trying to be a jerk, but you could have figured this out on your own.  (I always encourge self-help and docs-digging.)

Thanks for your time.

My guess is some of your code is executing twice or causing two removals.  

Let me make a few  suggestions.

  1. Never use ‘removeSelf()’.  Call ‘display.remove( self )’ instead.  SAFER.

  2. Consolidate your clean up in a finalize call

    – Automatically Clean up when removed colorMarble.finalize = function( self ) if( self.enterFrame ) then Runtime:removeEventListener( “enteFrame”, self ) self.enterFrame = nil end end; colorMarble:addEventListener( “finalize” )

  3. Here is the modified ‘enterFrame’ listener.

    – This is better function colorMarble.enterFrame( self ) if (self.y == nil) then return end if (self.y > display.contentHeight + 50) then display.remove( self ) end end; Runtime:addEventListener( “enterFrame”, colorMarble )

  4. Here is the modified touch listener

    function colorMarble.touch( self, event) if (event.phase == “began”) then display.remove( self ) end return false end; colorMarble:addEventListener( “touch” )

Thanks, I made the changes.

So now the original problem was the marbleTouched function (touching the marble onscreen will delete/remove it). So should I be calling colorMarble.finalize() inside that marbleTouched function?

Did you read the docs about the ‘finalize’ event before asking?

Answer: No.  The ‘finalize’ event is thrown for every object when it is deleted.  So, if you have a ‘finalize’ listener, it executes automatically when the object is removed. 

(re-posted from above)

-- Automatically Clean up when removed colorMarble.finalize = function( self ) if( self.enterFrame ) then Runtime:removeEventListener( "enteFrame", self ) self.enterFrame = nil end end; colorMarble:addEventListener( "finalize" )

You DO NOT call it manually.

 I’m not trying to be a jerk, but you could have figured this out on your own.  (I always encourge self-help and docs-digging.)

Thanks for your time.