Menu question

Hi, im making a game with multiples menues.
Lets say I have 4 buttons on the left of the game and each one (if pressed), loads an image on the bottom of the device, that image could be changed 3 more times (4 items per button, 16 in total)
I’m having some problems trying to achieve this.
I’ve tried groups (4 groups) but I had 2 problems:

  1. I dont know how to initialize an image without rendering it, I need it to insert it to the group and call a function later in the script.
  2. whenever I press the “next” button, the first image is still there, tried removeself, isvisible = false but nothing.

Basically this is the code (resumed):

[lua]local button_01_01 = display.newImage(“button_01_01.png”)
local button_01_02 = display.newImage(“button_01_02.png”)
local button_01_03 = display.newImage(“button_01_03.png”)
local button_01_04 = display.newImage(“button_01_04.png”)
local group01 = display.newGroup()
group01:insert (button_01_01)
group01:insert (button_01_02)
group01.x = -100; group01.y = -100
group01.isVisible = true

function press01 (event)
local menu01 = display.newImage(“button_01_01.png”)
menu01.x = 160
menu01.y = 428
group01[1].x = 250
group01[1].y = 520
function group001 (event)
group01[2].x = 250
group01[2].y = 520
end
button_ChangeRight:addEventListener(“touch”,group001)
end
button_01:addEventListener(“touch”,press01)
[/lua]

I could take some help over here, thanks in advance. [import]uid: 108461 topic_id: 20377 reply_id: 320377[/import]

I’m pretty sure your listener should look like this:

[lua]button_01_01:addEventListener(“touch”,press01)[/lua]

I feel like the way you are doing this is very complex and difficult. I would recommend using director class, it makes menus and scenes very simple and memory efficient.

Check it out here: http://developer.anscamobile.com/code/director-class-10

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 20377 reply_id: 79607[/import]

Thanks for the reply, I’ve managed to make it work using tables, last script was a mess.

Now I have another question, how can I get the following item in a table? If im requesting for example “menu[1]”, and I have more than 10 items in that table, what is the best way to get the next current one if it depends on an “touch” event?

Thanks. [import]uid: 108461 topic_id: 20377 reply_id: 79619[/import]

Bumping this question. [import]uid: 108461 topic_id: 20377 reply_id: 79841[/import]

@undecode,

I don’t know what exactly your after, but if you just want
to increment…some thing like this may work for ya or modify
to suit your specific needs in your custom menu…sounds like
that’s what your trying to do…this is really simple, might put you on the right track anyway…as long as it sheds a little light…:wink:

[code]
local _W = display.contentWidth;
local _H = display.contentHeight;

local touchButton = display.newRect( 50, 50, 100, 50 );
touchButton.x = _W / 2;
touchButton.y = _H / 2;

local myMenu = {};
local menuItem = 1;

for menuItem = 1, 10 do

myMenu[menuItem] = menuItem;

end
local function printmyMenu(e)

if e.phase == “ended” then

print("myMenu: ",myMenu[menuItem]);

menuItem = (menuItem + 1);

if menuItem == 11 then
menuItem = 1; – reset to 1 if over 10 menu items
end
end

return 1; – return what ever you want…
end

touchButton:addEventListener(“touch”, printmyMenu);

– end of line [import]uid: 107633 topic_id: 20377 reply_id: 79865[/import]

Thanks for the help sharp, but im using this:

[lua]local head = {“button_01_01.png”,“button_01_02.png”}[/lua]
and I dont know how to do it with strings. I know “button_01_01.png” is equal to head[1], but head[1+1] or something like that looks really wrong. I want to make something like that, accesing the next/last item, should I use a for loop?
Thanks again :slight_smile:
Edit: actually, “head[1+1]” works, lol. I’ll keep trying to get it to work. [import]uid: 108461 topic_id: 20377 reply_id: 79921[/import]

Well undecode,

This may help you then, like I mentioned above I don’t really
know what your trying to do (well kinda), but it’s a little
different for sure…here is some code for ya to play with
to get a little grip with arrays and tables…hope it helps
set you in the direction your looking for…:wink:

[code]local _W = display.contentWidth;
local _H = display.contentHeight;
local indexMenu = 1; --Lua arrays are 1-based: the first index is 1 rather than 0
local buttonList = {“button_01_02.png”, “button_01_03.png”, “button_01_03.png”}; --buttons
local menuItem = {“menu01”, “menu02”, “menu03”}; --placeholders for our buttons
local xLocation = {160, 256, 352}; --x locations for our buttons
local yLocation = {420, 468, 516}; --y locations for our buttons

– main button to use as a test for array below
local mainButton = display.newImage(“button_01_01.png”);
mainButton.x = _W / 2;
mainButton.y = 50;
local function pressMainButton(e)

if e.phase == “ended” then

if indexMenu <= #menuItem then --# is the length operator for tables and strings

menuItem[indexMenu] = display.newImage(buttonList[indexMenu]);
menuItem[indexMenu].x = xLocation[indexMenu];
menuItem[indexMenu].y = yLocation[indexMenu];
–print(menuItem[indexMenu].x); --for testing x position
–print(menuItem[indexMenu].y); --for testing y position
indexMenu = (indexMenu + 1);

end

end

end

mainButton:addEventListener(“touch”, pressMainButton);

– end of line
[import]uid: 107633 topic_id: 20377 reply_id: 80016[/import]

Sharp, thanks a lot man, you are awesome. Im working on it right now, but I did understand how it works. [import]uid: 108461 topic_id: 20377 reply_id: 80076[/import]

Ok, im not able to make the last/next buttons to work.
Its just bugged, here is the script (sorry its a long one, but pretty basic). If its too long, running it with the simulator might be better.
[lua]–indexmenues
head_IM = 2
shoes_IM = 2

–category buttons (left menu)
local button_01 = display.newImage(“button_01.png”)
button_01.x = 32
button_01.y = 134
local button_04 = display.newImage(“button_04.png”)
button_04.x = 32
button_04.y = 326

–inventory menu buttons (bottom menu)
local button_ChangeTotal = display.newImage(“button_ChangeTotal.png”)
button_ChangeTotal:setReferencePoint(display.TopLeftReferencePoint)
button_ChangeTotal.x = 40
button_ChangeTotal.y = 380

local button_ChangeRight = display.newImage(“button_ChangeRight.png”)
button_ChangeRight.x = 250
button_ChangeRight.y = 430

local button_ChangeLeft = display.newImage(“button_ChangeLeft.png”)
button_ChangeLeft.x = 72
button_ChangeLeft.y = 430

–head and shoes tables
local head = {“button_01_01.png”,“button_01_02.png”}
local shoes = {“button_04_01.png”,“button_04_02.png”,“button_04_03.png”,“button_04_04.png”}

–left buttons functions
local function press01(event)
local menu01 = display.newImage(head[1])
menu01.x = 160
menu01.y = 428
end
button_01:addEventListener(“touch”,press01)

local function press04 (event)
local menu04 = display.newImage(shoes[1])
menu04.x = 160
menu04.y = 428
end
button_04:addEventListener(“touch”,press04)

–next head function
local function nextHead (event)
if event.phase == “began” then
if head_IM <= #head then
head[head_IM] = display.newImage(head[head_IM])
head[head_IM].x = 160
head[head_IM].y = 428
head_IM = head_IM + 1
end
end
end
button_ChangeRight:addEventListener(“touch”,nextHead)
–back head function
local function backHead (event)
if event.phase == “began” then
if head_IM <= #head then
head[head_IM] = display.newImage(head[head_IM])
head[head_IM].x = 160
head[head_IM].y = 428
head_IM = head_IM -1
end
end
end
button_ChangeLeft:addEventListener(“touch”,backHead)
–next shoes function
local function nextShoes (event)
if event.phase == “began” then
if shoes_IM <= #shoes then
shoes[shoes_IM] = display.newImage(shoes[shoes_IM])
shoes[shoes_IM].x = 160
shoes[shoes_IM].y = 428
shoes_IM = shoes_IM +1
end
end
end
button_ChangeRight:addEventListener(“touch”,nextShoes)
–back shoes function
local function backShoes (event)
if event.phase == “began” then
if shoes_IM <= #shoes then
shoes[shoes_IM] = display.newImage(shoes[shoes_IM])
shoes[shoes_IM].x = 160
shoes[shoes_IM].y = 428
shoes_IM = shoes_IM -1
end
end
end
button_ChangeLeft:addEventListener(“touch”,backShoes)[/lua] [import]uid: 108461 topic_id: 20377 reply_id: 80104[/import]

Hey undecode,

Is there anyway you could zip up your graphic buttons and the
part of code that’s not working and I can take a closer look…

also remember when you ‘click’ on a button your going to get
a down click “began” phase and when your finger lets go your going to get a “ended” phase click…so you will get your functions running twice plus ALL code that increments will get a EXTRA addition ***i.e head_IM = (head_IM + 1) *** will
have 2 added instead of the +1 that you want if this makes any sense…I think just looking quickly at
this …it’s a good place to start looking…anywhooo.:wink:
Happy Coding :slight_smile:
Larry
email: sharp1959@comcast.net
[import]uid: 107633 topic_id: 20377 reply_id: 80180[/import]