RESTARTING SCENE MORE THAN TWICE

SCENE 2

local composer = require( “composer” )

local scene = composer.newScene()

local _H = display.viewableContentHeight;

local _W = display.viewableContentWidth;


– All code outside of the listener functions will only be executed ONCE 

– unless “composer.removeScene()” is called.


– local forward references should go here



– “scene:create()”


function scene:create( event )

  local sceneGroup = self.view

  

  – Initialize the scene here.

  – Example: add display objects to “sceneGroup”, add touch listeners, etc.

end


– “scene:show()”


function scene:show( event )

  local sceneGroup = self.view

  local phase = event.phase

  if ( phase == “will” ) then

    – Called when the scene is still off screen (but is about to come on screen).

  elseif ( phase == “did” ) then

  

  local physics = require(“physics”);

physics.start();

local bombTimer;

–STRIKES ACTUAL

removed = 0;

local function re()

if removed <4 then

if removed == 1 then

strike1.isVisible = true;

end

if removed == 2 then

strike2.isVisible = true;

end

if removed == 3 then

strike3.isVisible = true;

composer.gotoScene(“scene3”);

end

end

end

Runtime:addEventListener(“enterFrame”, re);

–BACKGROUND

local background = display.newImage(“bgr.jpg”)

background.x = display.contentCenterX

background.y = display.contentCenterY

background.height = _H+60;

background.width = _W;

sceneGroup:insert(background);

–KEEPING THE BALLS ON SCREEN AND BOUNCING AROUND

local wall1 = display.newRect(0,-25,_W+ _W,10);

physics.addBody(wall1, “static”);

sceneGroup:insert(wall1);

local wall2 = display.newRect(0,-10,10,_H+_H+_H);

physics.addBody(wall2, “static”);

sceneGroup:insert(wall2);

local wall3 = display.newRect(0,_H +40,_W+ _W,10);

physics.addBody(wall3, “static”);

sceneGroup:insert(wall3);

local wall4 = display.newRect(_W, -10,10, _H+_H+_H);

physics.addBody(wall4, “static”);

sceneGroup:insert(wall4);

–SORTING BOXES

boxRed = display.newRect(10,_H/2, 60,100);

sceneGroup:insert(boxRed);

box1 = display.newImage(“lol.jpg”,boxRed.x, boxRed.y);

box1.height = boxRed.height;

box1.width = boxRed.width;

sceneGroup:insert(box1);

cact = display.newImage (“abc.png”, boxRed.x+7, boxRed.y);

cact.height = boxRed.height/2

cact.width = boxRed.width/2

sceneGroup:insert(cact);

physics.addBody(boxRed, “static”);

boxBlue = display.newRect(_W-10,_H/2,60,100);

sceneGroup:insert(boxBlue);

box2 = display.newImage(“lol.jpg”,boxBlue.x, boxBlue.y);

box2.height = boxBlue.height;

box2.width = boxBlue.width;

sceneGroup:insert(box2);

cact1 = display.newImage (“abc.png”, boxBlue.x-7, boxBlue.y);

cact1:setFillColor(0,0.7,0);

cact1.height = boxBlue.height/2;

cact1.width = boxBlue.width/2;

sceneGroup:insert(cact1);

physics.addBody(boxBlue, “static”);

–POINTS START

points = 0;

psystem = display.newText(points, _W/2, 2,native.systemFont, 42)

psystem:setFillColor (0,1,0);

sceneGroup:insert(psystem);

–MAKES SURE THE POINTS ALWAYS UPDATED

function refresh()

psystem.text = (points);

end

Runtime:addEventListener(“enterFrame”, refresh);

–STRIKES

strike2 = display.newImage(“strike.png”, _W/2, 30);

strike2.height = 30; strike2.width = 30;

strike2.isVisible = false;

sceneGroup:insert(strike2);

strike3 = display.newImage(“strike.png”, strike2.x +35, 30);

strike3.height = 30; strike3.width = 30;

strike3.isVisible = false;

sceneGroup:insert(strike3);

strike1 = display.newImage(“strike.png”, strike2.x - 35, 30);

strike1.height = 30; strike1.width = 30;

strike1.isVisible = false;

sceneGroup:insert(strike1);

  local function spawn()

physics.setGravity(math.random(0,1), 2);

number = 1;

tf = math.random(0,1);

hold = false;

–SPAWNING THE BALLS

for i=1, number do 

–THE CACTUS SPRITE

local options = {

width = 225/3,

height = 161,

numFrames = 3

}

local ballSheet = graphics.newImageSheet(“ok.png”,options);

local ballSequenceData = {

name = “moving”,

start = 1,

count = 3,

time = 400,

loopCount = 0,

loopDirection = “forward”

}

local ball = display.newSprite(ballSheet, ballSequenceData);

ylocation = math.random(0,1);

ball.x = _W/2;

if ylocation == 0 then

ball.y = _H - 30;

else

ball.y = 30;

end

ball:setSequence(“moving”);

ball:play();

physics.addBody(ball, {bounce=1});

ball.type = “ballBlue”

ball.isFixedRotation = true;

sceneGroup:insert(ball);

–MOVING THE BALLS

local function move(event)

local objectTouched = event.target;

if event.phase == “began” then

objectTouched.previousX = objectTouched.x;

objectTouched.previousY = objectTouched.y;

elseif event.phase == “moved” then

objectTouched.x = (event.x - event.xStart) + objectTouched.previousX;

objectTouched.y = (event.y - event.yStart) + objectTouched.previousY;

hold = true;

elseif event.phase == “ended” then

hold = false;

end

end

ball:addEventListener(“touch”,move);

–TIMING OUT THE BALLS

local function flash1()

ball.isVisible = false;

local function flash2()

ball.isVisible = true;

bombTimer = timer.performWithDelay(600, flash1)

end

bombTimer = timer.performWithDelay(80, flash2)

end

bombTimer = timer.performWithDelay(3000, flash1); 

local function test()

if ball.type ~= “gone” and strike3.isVisible == false then

removed = removed + 1;

print(removed);

ball:removeSelf()

end

end

bombTimer = timer.performWithDelay(6000, test, 1)

bombTimer = timer.performWithDelay(3000, flash)

–SORTING BIN SCRIPT

local function sort(event)

if event.phase == “began” then

if event.other.type == “ballRed” and hold == true then

print(“cool”);

event.other:removeSelf();

points = points +1;

event.other.type = “gone”

end

end

end

boxRed:addEventListener(“collision”, sort);

local function sort(event)

if event.phase == “began” then

if event.other.type == “ballBlue” and hold == true then

event.other:removeSelf();

points = points +1;

event.other.type = “gone”

end

end

end

boxBlue:addEventListener(“collision”, sort);

–CHANGING BALL COLOR

if tf == 0 then

–ball:setFillColor( .75, 0, 0 );

ball.type = “ballRed”;

bombTimer = timer.performWithDelay(4000, flash);

else

ball:setFillColor(0,0.7,0);

ball.type = “ballBlue”;

bombTimer = timer.performWithDelay(4000, flash);

end

end

end

bombTimer = timer.performWithDelay(1500, spawn, 5);

bombTimer = timer.performWithDelay(2500, spawn, 5);

  end

end


– “scene:hide()”


function scene:hide( event )

  local sceneGroup = self.view

  local phase = event.phase

  if ( phase == “will” ) then

    – Called when the scene is on screen (but is about to go off screen).

    – Insert code here to “pause” the scene.

    – Example: stop timers, stop animation, stop audio, etc.

if bombTimer then

timer.cancel(bombTimer)

bombTimer = nil end

removed = 0

print(removed)

  elseif ( phase == “did” ) then

    – Called immediately after scene goes off screen.

  end

end


– “scene:destroy()”


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;

SCENE 1

local composer = require( “composer” )

local scene = composer.newScene()


– Code outside of the scene event functions below will only be executed ONCE unless

– the scene is removed entirely (not recycled) via “composer.removeScene()”



– Scene event functions


 local function loadscene()

db = false;

composer.removeScene(“scene1”)

composer.gotoScene(“scene1”);

end

– create()

function scene:create( event )

    local sceneGroup = self.view

end

– show()

function scene:show( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

        – Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == “did” ) then

        – Code here runs when the scene is entirely on screen

local _H = display.viewableContentHeight;

local _W = display.viewableContentWidth;

db = true;

–BACKGROUND

local background = display.newImage(“open.jpg”)

background.x = display.contentCenterX

background.y = display.contentCenterY

background.height = _H+60;

background.width = _W;

sceneGroup:insert(background);

–BACKGROUND

local b = display.newImage(“game.png”)

b.x = _W/2

b.y = _H/2-50

b.height = 180;

b.width = 260;

sceneGroup:insert(b);

–CACTUS

local options = {

width = 225/3,

height = 161,

numFrames = 3

}

local ballSheet = graphics.newImageSheet(“ok.png”,options);

local ballSequenceData = {

name = “moving”,

start = 1,

count = 3,

time = 400,

loopCount = 0,

loopDirection = “forward”

}

local ball = display.newSprite(ballSheet, ballSequenceData);

ball.x = _W/2+100;

ball.y = _H-100;

ball:setSequence(“moving”);

ball:play();

sceneGroup:insert(ball);

–START BUTTON

local board = display.newRect(_W/2,_H/5,160,120)

local start = display.newText(“RESTART”, board.x, board.y);

start:setFillColor (0,0,1);

sceneGroup:insert(board);

sceneGroup:insert(start);

 board:addEventListener(“touch”, loadscene);

end

 end

– hide()

function scene:hide( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then

        – Code here runs immediately after the scene goes entirely off screen

    end

end

– destroy()

function scene:destroy( event )

    local sceneGroup = self.view

    – Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene;

The code runs main.lue --> scene2 --> scene1 --> scene2

The problem I’m having is that the code will run twice and then the third time the error [scene.lua:71: attempt to index local ‘sceneGroup’ (a nil value)] comes up. The line that is listed as an error is

 sceneGroup:insert(background);

My guess is that the display objects are stacking up on each other and can’t load in more than twice hence why I added 

composer.removeScene(“scene1”)

composer.gotoScene(“scene1”);

Can anyone help me make the game run smoothly and restart an infinite amount of times without problems?

HI @patriciambac

I would suggest that you take a look at the composer example that comes with Corona SDK.  You should find it here:

c:\Program Files (x86)\Corona Labs\Corona SDK\Sample Code\Interface\Composer

I found this example very useful.

There are several things to discuss. First and foremost, you’re not going to get a lot of help if you dump a lot of code into a post in particular if you did not use code formatting. The blue <> button in the row with Bold, Italic, etc. will popup a window that you can paste your code into and it will maintain your code formatting. 

Speaking of code formatting, it’s very important to maintain proper indention of your code blocks so people can see where functions begin and end when helping you.  See: https://docs.coronalabs.com/tutorial/basics/codeFormatting/index.html

Once we have code that we can read, you will find better responses.

Next on to your code. There are several things that are kind if red-flags.  First you’re doing almost no work in scene:create(). You’re doing almost everything in scene:show()'s “did” phase. This typically happens when you are having scope issues. (https://docs.coronalabs.com/tutorial/basics/scope/index.html). When you do this, you pretty much remove your ability to use transitions to fade in a scene. You also create a situation where your code is hard to manage.

We put all the comments in the template to help you know where to put items.  Any object that should be on the screen when the scene starts should be created in scene:create(). Objects that need to appear once play begins like spawns can be started in scene:show()'s did phase, but the code to do the spawning should be in functions above the scene:create() function.

If you have objects that need to be accessed in scene:create, scene:show() or other functions declare them local at the top of the module.

Finally you cannot remove the scene you are in. You have to either a) go to another scene in which case you can remove the scene in it’s scene:hide()'s “did” phase or b) restart the scene by repositioning objects that moved during game play, resenting scores, lives, etc.  

Can you make it work the way you’re doing it? Sure  you can put everything in scene:show()'s “did” phase but it’s going to be harder to support in the long run. I would also consider looking at our getting started guide – in particular chapter’s 4 and 5.  https://docs.coronalabs.com/guide/programming/index.html

This will be a good guide on how to build a Composer based game.

Rob

There are lots of details…Need to familiarize such things. Thank for the information.

HI @patriciambac

I would suggest that you take a look at the composer example that comes with Corona SDK.  You should find it here:

c:\Program Files (x86)\Corona Labs\Corona SDK\Sample Code\Interface\Composer

I found this example very useful.

There are several things to discuss. First and foremost, you’re not going to get a lot of help if you dump a lot of code into a post in particular if you did not use code formatting. The blue <> button in the row with Bold, Italic, etc. will popup a window that you can paste your code into and it will maintain your code formatting. 

Speaking of code formatting, it’s very important to maintain proper indention of your code blocks so people can see where functions begin and end when helping you.  See: https://docs.coronalabs.com/tutorial/basics/codeFormatting/index.html

Once we have code that we can read, you will find better responses.

Next on to your code. There are several things that are kind if red-flags.  First you’re doing almost no work in scene:create(). You’re doing almost everything in scene:show()'s “did” phase. This typically happens when you are having scope issues. (https://docs.coronalabs.com/tutorial/basics/scope/index.html). When you do this, you pretty much remove your ability to use transitions to fade in a scene. You also create a situation where your code is hard to manage.

We put all the comments in the template to help you know where to put items.  Any object that should be on the screen when the scene starts should be created in scene:create(). Objects that need to appear once play begins like spawns can be started in scene:show()'s did phase, but the code to do the spawning should be in functions above the scene:create() function.

If you have objects that need to be accessed in scene:create, scene:show() or other functions declare them local at the top of the module.

Finally you cannot remove the scene you are in. You have to either a) go to another scene in which case you can remove the scene in it’s scene:hide()'s “did” phase or b) restart the scene by repositioning objects that moved during game play, resenting scores, lives, etc.  

Can you make it work the way you’re doing it? Sure  you can put everything in scene:show()'s “did” phase but it’s going to be harder to support in the long run. I would also consider looking at our getting started guide – in particular chapter’s 4 and 5.  https://docs.coronalabs.com/guide/programming/index.html

This will be a good guide on how to build a Composer based game.

Rob

There are lots of details…Need to familiarize such things. Thank for the information.