How can a function read a variable from the sceneGroup?

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?

First, it’s very helpful if you post formatted code. Use the <> button in the editor, paste your code into the window that shows up. It’s also important for us to know what line 36 is.

This feels like a scope problem.  You might want to also read this tutorial:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob

Hi mariom

I’m a beginner and I got to this post with a similar problem, I read your code and I don´t understand why you what the sceneGroup in the firebullet function. It appears you want to create a bullet and insert the bullet in the sceneGroup.

The problem that I was facing was that  I what to create an object and include it in the sceneGruop but I couldn´t insert it in the sceneGroup because the function was not in one of the scene:create or scene:show function

After read some example code I found the solution. 

In the external function you have to create a local sceneGroup from the scene and then insert it, for example in your code, it would be something like this:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity(0, 0); physics.pause() local function fireBullet(xpos, ypos) local sceneGroup = scene.view local bullet= display.newRect(0, 0, 40, 40) bullet.x, bullet.y= xpos, ypos physics.addBody(bullet) sceneGroup:insert ( bullet) return true end 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

I hope this helps

First, it’s very helpful if you post formatted code. Use the <> button in the editor, paste your code into the window that shows up. It’s also important for us to know what line 36 is.

This feels like a scope problem.  You might want to also read this tutorial:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob

Hi mariom

I’m a beginner and I got to this post with a similar problem, I read your code and I don´t understand why you what the sceneGroup in the firebullet function. It appears you want to create a bullet and insert the bullet in the sceneGroup.

The problem that I was facing was that  I what to create an object and include it in the sceneGruop but I couldn´t insert it in the sceneGroup because the function was not in one of the scene:create or scene:show function

After read some example code I found the solution. 

In the external function you have to create a local sceneGroup from the scene and then insert it, for example in your code, it would be something like this:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity(0, 0); physics.pause() local function fireBullet(xpos, ypos) local sceneGroup = scene.view local bullet= display.newRect(0, 0, 40, 40) bullet.x, bullet.y= xpos, ypos physics.addBody(bullet) sceneGroup:insert ( bullet) return true end 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

I hope this helps