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!