Difficulties with scenes

Hello, i am trying to create a card game.

I have 3 modules, one that randomly creates the opponent and randomly presents 3 cards on the screen for the player to choose. Once the player picks a card, the game will jump to the 2nd module that will enable the opponent to play and will appear a message on the screen that the opponent is playing. Once that executes it will jump to a 3rd module that will present the player again with 3 random cards and will increase the number of rounds.

My issue is… once the game reaches the 3rd module where the player is presented again with three cards it will not execute the commands that are supposed to be executed once the player pushes on the card.

I have created a new project as a sample…The code is the following:

main.lua

local storyboard = require(“storyboard”)

local background = display.newImage(“Icon-72.png”)
storyboard.gotoScene(“level1”)

level1.lua

local storyboard = require(“storyboard”)
local level1 = storyboard.newScene()

function level1:createScene( event )
    print(“level 1 create scene”)
    local group = self.view
    local x = 3
    group:insert(display.newText(x,40,50))
    – body

    local card = display.newImage(“Icon-Small.png”)
    card.x = 50 ; card.y = 150
    group:insert(card)
    function card:touch(event )
        display.remove(card)
        storyboard.gotoScene(“level2”)
    end

    card:addEventListener(“touch”,card)
end

function level1:enterScene( event )
    local group = self.view
    local card = display.newImage(“Icon-Small.png”)
    card.x = 50 ; card.y = 150
    group:insert(card)
    function card:touch(event )
        display.remove(card)
        storyboard.gotoScene(“level2”)
    end

    card:addEventListener(“touch”,card)
    – body
end
level1:addEventListener(“createScene”,level1)
level1:addEventListener(“enterScene”,level1)
return level1

level2.lua

local storyboard = require(“storyboard”)
local level2 = storyboard.newScene()

function level2:createScene( event )
    print(“level2 create”)
    local group = self.view
    
    storyboard.purgeScene(“level1”)
    storyboard.gotoScene(“level1”)
    
end

function level2:exitScene(event )
    --storyboard.gotoScene(“level1”)
    – body
end

level2:addEventListener(“createScene”,level2)
level2:addEventListener(“exitScene”,level2)
return level2

Please help

You seem to have some rather odd duplication here. you are creating a card both in the createScene and the enterScene. I’m guessing that you want a new card every time you enter that scene, in which case it belongs in enterScene really.

Think of it as a rotating stage ; when you ‘createScene’ it is a bit like adding the scenery :slight_smile: and ‘enterScene’ is going to that scene, dressing it a bit and putting the actors on.

You don’t need to tear down the scene between scene changes - you can if you want to, or the system deems it a good idea (reclaiming resources), but it isn’t mandatory.

Have a look at http://coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/ 

If you remove the createScene stuff and the purgeScene call in level2 it should work fine.

Two other points ; be a bit careful about creating closures - local functions in local functions. You can end up dragging your own context about.

Also, whilst I think it will work it’s also a good idea to be careful about putting gotoScene() calls in the chain for other gotoScene calls - when you do gotoScene it does something like

  1. send an event
  2. close the scene
  3. send another event
  4. send create scene
  5. send pre-open message (forgotten what it is)
  6. transition
  7. send post-open message

it’s a finite state machine at heart :slight_smile: If you send a gotoScene in the createScene or enterScene then if you aren’t careful you can have all kinds of chaotic cross effects. It might work I think, but switching state (scene) in the middle of another switch can cause problems.

Also have a look at Composer, I think Storyboard may be removed at some point. It is very similar.

You seem to have some rather odd duplication here. you are creating a card both in the createScene and the enterScene. I’m guessing that you want a new card every time you enter that scene, in which case it belongs in enterScene really.

Think of it as a rotating stage ; when you ‘createScene’ it is a bit like adding the scenery :slight_smile: and ‘enterScene’ is going to that scene, dressing it a bit and putting the actors on.

You don’t need to tear down the scene between scene changes - you can if you want to, or the system deems it a good idea (reclaiming resources), but it isn’t mandatory.

Have a look at http://coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/ 

If you remove the createScene stuff and the purgeScene call in level2 it should work fine.

Two other points ; be a bit careful about creating closures - local functions in local functions. You can end up dragging your own context about.

Also, whilst I think it will work it’s also a good idea to be careful about putting gotoScene() calls in the chain for other gotoScene calls - when you do gotoScene it does something like

  1. send an event
  2. close the scene
  3. send another event
  4. send create scene
  5. send pre-open message (forgotten what it is)
  6. transition
  7. send post-open message

it’s a finite state machine at heart :slight_smile: If you send a gotoScene in the createScene or enterScene then if you aren’t careful you can have all kinds of chaotic cross effects. It might work I think, but switching state (scene) in the middle of another switch can cause problems.

Also have a look at Composer, I think Storyboard may be removed at some point. It is very similar.