Im a total novice and I wanted to make a game.lua file that uses a require “marbles.lua” which contains functions I would need to spawn marbles (from top, falling to bottom). I’m not entire sure how to do this, but this is what I ended up doing. Any advice would help.
game.lua:
--------------------------------------------------------------------------------- -- -- scene.lua -- --------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) local physics = require("physics") local marbles = require("marbles") local marbleHandler -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local bkg = display.newImage("images/Default-568h@2x.png", display.contentWidth\*0.5, display.contentHeight\*0.5 ) marbleHandler = marbles.new() end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then physics.start() marbleHandler.addNewMarble() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene
Now marble.lua
local M = {} function M.new(listener, options) -- make a new three match board options = options or {} local difficulty = options.difficulty or 1 local x,y = options.x or math.random(display.contentWidth\*0.1, display.contentHeight\*0.9), options.y or -200 local w,h = options.w or 70, 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 blackMarbleTouched(event) if (event.phase == "began") then Runtime.removeEventListener("enterFrame", event.self) event.target:removeSelf() --score = math.floor(score \* 0.5) --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) self:removeSelf() end end function addNewMarble() if(math.random(1,5)==1) then --Black Marble local blackMarble = display.newImage( "images/5.png", x, y) blackMarble.width = w; blackMarble.height = h; physics.addBody(blackMarble) blackMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", blackMarble) blackMarble:addEventListener("touch", marbleTouched) else --Normal Marble local redMarble = display.newImage("images/4.png", x, y) redMarble.width = w; redMarble.height = h; physics.addBody(redMarble) redMarble.enterFrame = offscreen Runtime:addEventListener("enterFrame", redMarble) redMarble:addEventListener("touch", marbleTouched) end end end return M
I’ve run into a few errors, and I couldn’t find tutorials guidelines on how to accomplish this, but here’s the error I’m getting:
13:01:30.106 ERROR: Runtime error 13:01:30.106 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:45: attempt to index upvalue 'marbleHandler' (a nil value) 13:01:30.106 stack traceback: 13:01:30.106 C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:45: in function \<C:\Users\Didier\Documents\Corona Projects\Chaos Marbles\game.lua:32\> 13:01:30.106 ?: in function 'dispatchEvent' 13:01:30.106 ?: in function \<?:865\> 13:01:30.106 (tail call): ? 13:01:30.106 ?: in function \<?:504\> 13:01:30.106 ?: in function \<?:169\>