Sorry for the multiple answer, but I wanted to also suggest making a few simple examples using composer.* before using it for game work.
You need to have a solid grasp of how it works and when the various functions are called as well as the concept of will and did phases for show() and hide().
When you fully understand these things, using composer.* will make a lot more sense and be less mysterious.
At this time I can see you’re totally ignoring or not aware of the will/did phases. This leads me to believe you’re treating composer.* exactly like storyboard.* and they are two very different beasts.
-- example1.lua local M = {} print( "example1.lua has been loaded" ) return M -- main.lua local ex1 = require( "example1" ) ex1.testvar = "Hello World!"
That worked . I got example1.lua has been loaded in the simulator .
I tried the first test you gave me and this is what I did :
Main.lua : local M = {} local example1 = require "example1" -- or whatever you named the module file. example1.create( display.currentStage ) timer.performWithDelay( 1000, example1.destroy ) -- If this doesn't crash you're ready for the next test. return M
example1.lua :
– example1.lua
local M = {}
return M
I’m getting this error :
main.lua:8: attempt to call field 'create' (a nil value)
Let me put together a small example of what the module should look like and I’ll post it so you can use it as a framework to scaffold your code onto.
-Ed
PS - Welcome back. I know you’re in and out of the forums and I sense you’ve been struggling and a bit frustrated at times. This time I think you’ll make some progress and get some satisfaction out of the experience.
001_starter - A bare bones module with create(), start(), stop(), destroy() functions and some basic parts I think you should always have.
002_sample_game - OK, not really a game, but more like a tech demo showing how to make something very simple with the module. Go into main.lua and experiment with the start, stop, destroy calls too.
003_integrated_in_composer - Basically 002 put into a composer scene.
Warning: This composer.* scene uses my custom format to separate the will and did phases of show() and hide() into separate function calls. So be aware it is a little different from the default sample you see on the Corona site.
One last note. I know I never answered your ORIGINAL question.
That was on purpose.
I think the code for your gun is something you can solve. The real problem is that I think you’re having some issues with scope and visibility as well as composer.* concepts.
I think that if you use the module first approach you’ll self-solve the gun problem and be in a much better place for future work.
In my main.lua file I put what was in the example’s main.lua file :
local example1 = require "example1" local group = display.newGroup() example1.create( group ) timer.performWithDelay( 4000, function() example1.start() end ) timer.performWithDelay( 8000, function() example1.stop() end ) timer.performWithDelay( 12000, function() example1.destroy() end )
and in my example1.lua file I put what was in the example’s game.lua file :
local physics = require "physics" local cx, cy = display.contentCenterX, display.contentCenterY local fullw, fullh = display.actualContentWidth, display.actualContentHeight local isRunning = false local layers local game = {} function game.create( group ) game.destroy() layers = display.newGroup() layers.underlay = display.newGroup() layers.content = display.newGroup() layers.overlay = display.newGroup() -- group:insert( layers ) -- This group contains all layers layers:insert( layers.underlay ) -- Bottom layer. layers:insert( layers.content ) -- Middle layer layers:insert( layers.overlay ) -- Top layer end etc...
When I did that I got nothing . Nothing should up on the simulator or in the console . I’m guessing everything went well .
When I did this in my main.lua file :
local example1 = require "example1" -- or whatever you named the module file. example1.create( display.currentStage ) timer.performWithDelay( 1000, example1.destroy ) -- If this doesn't crash you're ready for the next test.
and I left the game file the same, I don’t have any problems . Should I move on or should I be seeing something on my screen ?
You are displaying a still image, I don’t think Corona supports gifs in images. If you go online you could find software that splits gifs into sprites.