Hello guys.
This is my first topic here, and I feel like this is probably a stupid question, but I’m coming to Corona/ Lua from a different programming language(Java), and can’t seem to wrap my head around how something as simple as what I want to acomplish is done around here.
The idea is that there is a character in the bottom of the screen (shooter), and the touch listener triggers a function that will spawn a bullet right on top of this shooter. So it will need to take the shooter’s X and Y value for reference:
----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start() physics.setGravity(0, 0); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) local sceneGroup = self.view local shooter= display.newRect(screenW/2, screenH, screenW/10, screenH/5) shooter:setFillColor( 0, 0.63, 0.90) physics.addBody(shooter) physics.setDrawMode( "hybrid" ) sceneGroup:insert( shooter ) end function fireBullet(sceneGroup) local bullet= display.newRect(0, 0, 40, 40) bullet.x, bullet.y= sceneGroup.shooter.x, sceneGroup.shooter.y- sceneGroup.shooter.height/2 -40 physics.addBody(bullet) end local touchListener = function( event ) fireBullet() end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) Runtime:addEventListener( "touch", touchListener ) ----------------------------------------------------------------------------------------- return scene
I then receive the error message as the image shows, telling me that the sceneGroup in nil. So the question is: how can I make a function such as fireBullet be able to see whats going on in the sceneGroup, as if the group was a global variable from the document?