Character select

If I have main lua file in which I have two buttons, button one sets the character image file to character1 and button two sets the character image file to character2. How can I display the selected character in my game.lua using display.newImageRect? 

There are many ways for modules to talk to each other. They include:

  1. Use a data module.  

  2. Use Composer to pass parameters when calling composer.gotoScene()

  3. Use Composer’s setVariable and getVariable API calls

Personally, I use #1. It’s outlined here:  https://docs.coronalabs.com/tutorial/basics/globals/index.html#data-module

I should point out, main.lua isn’t a scene. It’s an initialization point and a place to call code to go to your first Composer scene.  UI elements like character select buttons probably should be in its own Composer scene.  Perhaps you should have a “menu.lua” or “characterselect.lua” file that’s a full Composer scene that main.lua simply calls composer.gotoScene( “menu” ).

Rob

How do  I store " display.newImageRect(parameters) " in a variable? The character is a “nil value” when I try to store the variable that contains " display.newImageRect(parameters) " in the character variable.

display.newImageRect() returns a display object, which at the top level is a Lua table, but it contains data that can’t be “saved”. But you should be able to pass the table’s address into other scenes. What is returned by display.newImageRect() won’t be nil unless it failed to create the object in the first place.

Rob

There are many ways for modules to talk to each other. They include:

  1. Use a data module.  

  2. Use Composer to pass parameters when calling composer.gotoScene()

  3. Use Composer’s setVariable and getVariable API calls

Personally, I use #1. It’s outlined here:  https://docs.coronalabs.com/tutorial/basics/globals/index.html#data-module

I should point out, main.lua isn’t a scene. It’s an initialization point and a place to call code to go to your first Composer scene.  UI elements like character select buttons probably should be in its own Composer scene.  Perhaps you should have a “menu.lua” or “characterselect.lua” file that’s a full Composer scene that main.lua simply calls composer.gotoScene( “menu” ).

Rob

How do  I store " display.newImageRect(parameters) " in a variable? The character is a “nil value” when I try to store the variable that contains " display.newImageRect(parameters) " in the character variable.

display.newImageRect() returns a display object, which at the top level is a Lua table, but it contains data that can’t be “saved”. But you should be able to pass the table’s address into other scenes. What is returned by display.newImageRect() won’t be nil unless it failed to create the object in the first place.

Rob