I am trying to print “Level Loaded” after a level is loaded, but it doesn’t wait for the level to load, it just prints it right away.
Inside loadLevel(1) there is a 5 second timer. My print statement shouldn’t print until after the 5 seconds. Why does it do this? It prints right when the level starts loading
[code]
function startGame(e)
if(e.phase == “ended”) then
loadLevel(1); --Load level, Wait 5 seconds, then print
print “Level Loaded”;
end
–spawnTimer = timer.performWithDelay(2000 - spawnRate * 100, spawnObjects, 1);
end
First, you didn’t post the loadlevel function so it is hard to guess what you do there. If that function contains only producual code, then your print command will be executed ones the function call to loadLevel returns.
Generally Corona is event based. A timer function tells the system to do something at a certain time and maybe repeat it in specified intervalls. It doesn’t stop the execution of the rest but goes further in the script. A good way to manage sonething like this is using the finite state machine and let it act on certain status which are set through events and timers. Of course there are other ways of doing it too.
local function loadLevel(level)
if(level == 1) then
correctType.text = "Color";
--slide Main menu to the left
transition.to( mainMenu, { time=400, x=(-999), y=(SCREEN\_HEIGHT / 2) } );
--make gameScreen1 visible and slide it into view
gameScreen1.alpha = 1;
transition.to( gameScreen1, { time=400, x=(0), y=(0) } );
end
delayTimer = timer.performWithDelay(1000, delay, 3);
end
Here is Delay and PreCount:
[code]
local function delay(e)
if(delayCount==0) then
preTimer = timer.performWithDelay(1000, preCount, 3);
else
delayCount = delayCount - 1;
end
end
–decreases 3,2,1 timer before level starts
local function preCount(e)
if(preCountDown > 1) then
preCountDown = preCountDown - 1;
preCountDownText.text = preCountDown;
elseif(preCountDown == 1) then
preCountDownText.alpha = 0;
resetCountDowns();
readyToPlay = true;
end
end
[lua]function startGame(e)
if(e.phase == “ended”) then
loadLevel(1); --Load level, Wait 5 seconds, then print
game:addEventListener(“levelLoaded”, onLevelLoaded)
end
end
[lua]elseif(preCountDown == 1) then
preCountDownText.alpha = 0;
resetCountDowns();
readyToPlay = true;
– i’m not sure about this target
– you’ll have to set it up for your system
local event = { name=“levelLoaded”, target=game }
game:dispatchEvent( event )
end[/lua]
i dont know what objects you have available and where the code is… if theyre in different modules maybe you will need to use [lua]Runtime:[/lua] rather than [lua]game:[/lua] etc
I used the “game” approach and that did exactly what I needed it to do. It is printing the text at the proper time. The only thing is that after it prints the text, I get this error:
Runtime error:
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘?’
?: in function <?:214>
Do you know what this error means? [import]uid: 39238 topic_id: 6877 reply_id: 24051[/import]
whatever you’re referencing after that doesn’t exist. can you post the bit of code that’s likely generating that? to narrow it down normally i do something like this