function question

Hi, im having some problems with a function.

I have two tables, 2 buttons(next and back) and 4 images for each table.
Next button is working, but I cant get the back button to work.

Wich is the best/easiest way to do this?
Thanks in advance [import]uid: 108461 topic_id: 20617 reply_id: 320617[/import]

I think we need a bit more information.
What does your Next button code look like? [import]uid: 108660 topic_id: 20617 reply_id: 80899[/import]

Can you post the relevant code? [import]uid: 19626 topic_id: 20617 reply_id: 80901[/import]

Yeah post up some code and we can help :slight_smile: [import]uid: 84637 topic_id: 20617 reply_id: 80906[/import]

Sorry, here it is:

Relevant code:
[lua]head_IM = 1
local head = {“button_01_01.png”,“button_01_02.png”}[/lua]

  1. Next button, working fine:

[lua]local function nextHead (event)
if event.phase == “began” then
if head_IM < #head then
head_IM = head_IM + 1
head[head_IM] = display.newImage(head[head_IM])
head[head_IM].x = 160
head[head_IM].y = 428
print(“nextHead”)
end
end
end
headGroup[1]:addEventListener(“touch”,nextHead) --headgroup is a group with back and next buttons[/lua]

  1. But back button, isn’t working:

[lua]local function backHead (event)
if event.phase == “began” then
if head_IM <= #head then
head_IM = head_IM - 1
head[head_IM] = display.newImage(head[head_IM])
head[head_IM].x = 160
head[head_IM].y = 428
–print(“backHead”)
end
end
end
headGroup[2]:addEventListener(“touch”,backHead)[/lua]
I’m not using director because i want to do everything in the same stage.

On a side note, how do you cycle the table? if i reach #head, how do i start from head[1] again? couldn’t make it work with that logic.
Thanks
[import]uid: 108461 topic_id: 20617 reply_id: 81049[/import]

Well one problem I see off the bat is that you’re using the table “head” for two purposes.

It starts out as an array if image file names. But in nextHead and backhead you are overwriting the values with display objects that show the graphic so that the next time you call display.newImageRect() you are passing a display object as the filename and that wont work.

The reason it’s working for the next button is you haven’t overwritten the filename yet. But when you hit the back button, head[1] isn’t a filename any more.

That is likely the cause.

for your second question:

head\_IM = head\_IM + 1  
if head\_IM \> #head then head\_IM = 1 end  
  
...  
  
head\_IM = head\_IM - 1  
if head\_IM \< 1 then head\_IM = #head end  
  

[import]uid: 19626 topic_id: 20617 reply_id: 81051[/import]

Thanks Rob.
Then what should i do? regarding the tables.

I tried with that code before and it didn’t work, i think it was because i still have the table problem, i’ll try to fix that thing first. [import]uid: 108461 topic_id: 20617 reply_id: 81055[/import]

Use two different tables.

head\_IM = 1  
local headImageFiles = {"button\_01\_01.png","button\_01\_02.png"}  
  
local function nextHead (event)  
 if event.phase == "began" then  
 if head\_IM \< #head then  
 head\_IM = head\_IM + 1  
 head[head\_IM] = display.newImage(headImageFiles[head\_IM])  
 head[head\_IM].x = 160  
 head[head\_IM].y = 428  
 print("nextHead")  
 end  
 end  
end  
headGroup[1]:addEventListener("touch",nextHead) --headgroup is a group with back and next buttons  

And the same for the other.

Also I never see where you add the the head[head_IM] into the display group. Also normally the event listener would be added to the object not the display group. I’m a bit confused by you having two display groups, and a comment saying that each group has back and next buttons.

[import]uid: 19626 topic_id: 20617 reply_id: 81057[/import]

Rob, I have more code but it has over 200 lines of code, thats why some stuff might be useless or bad optimized (well, this could be the case haha). In that particular case, I use groups to use isVisible false in the other ones whenever i press a category menu.

basically i have 4 buttons, each one has its own menu (menu, next, back, displays). Like if you were playing a RPG game, and you choose “head” “chest”, you know what im saying?

About the last piece of code, did you forget
[lua]head = {}[/lua]

Sorry if you didnt, but the only difference i see between the first one and this one is the table’s name.
Thanks a lot. [import]uid: 108461 topic_id: 20617 reply_id: 81061[/import]

yea, I forgot to add the head = {}

[import]uid: 19626 topic_id: 20617 reply_id: 81063[/import]

yea, I forgot to add the head = {}

[import]uid: 19626 topic_id: 20617 reply_id: 81064[/import]