user1790813
user1790813

Reputation: 704

Corona SDK widget.newButton() not appearing in simulator when inside scene:create

I have the following code:

function scene:create( event )

  local sceneGroup = self.view

  -- Initialize the scene here.
  -- Example: add display objects to "sceneGroup", add touch listeners, etc.

  local options1 = {
    x = display.contentWidth / 2,
    y = 200,
    onRelease = button,
    lableAlign = "center",
    emboss = true,
    id = "1",
    label = "Button1"
  }

  local options2 = {
    y = 300,
    x = display.contentWidth / 2,
    onRelease = button,
    lableAlign = "center",
    emboss = true,
    id = "2",
    label = "Button2"
  }

  local button1 = widget.newButton(options1)
  local button2 = widget.newButton(options2)
  sceneGroup:insert(button1)
  sceneGroup:insert(button2)
end

When I place this code in a standalone file (not a scene) the buttons show up as supposed. However now I am turning this standalone file into a scene and for some reason the above code results in nothing in the simulator. Any ideas?

Upvotes: 2

Views: 588

Answers (2)

Oliver
Oliver

Reputation: 29483

You have two choices:

  1. move your scene the a yourScene.lua file (named as you wish) in which you call storyboard.newScene() (no argument) and in your main.lua you use storyboard.goto('yourScene')
  2. you can create the scene in main.lua via storyboard.newScene('yourScene') and goto it from within main.lua via storyboard.goto('yourScene')

Basically your scene can be in a separate module, corona automatically names it based on module name, and main.lua goes to it, or your scene can be in same module, but then you have to give it a name yourself, and main.lua can "goto" it (within same module). You can even have multiple scenes in same module.

I recommend you have your scenes in separate modules, it just makes for better modularization. Conjecture: maybe the method 2 was the original, and method 1 was added latter when developers found their scenes were getting large and so needed to move them to separate modules.

Upvotes: 3

user1790813
user1790813

Reputation: 704

The code posted above is within the main.lua file. Therefore, the scene is never called. The solution is to rename the file above to something like menu.lua and then call this scene from main.lua.

Upvotes: 2

Related Questions