Okay, so I’m completely lost here. I’m trying to figure out how to make a menu that I can manipulate with arrow keys and, later, a controller. None of the options are meant to lead to anywhere yet. Here’s one version of the code:
-- Updates the location of the box showing the current menu selection local function changeSelect(currentSelect) menuSelect.y = 100 + (currentSelect \* 100) end -- Receives a key event. If the "up" arrow key is pressed, the menu selection will move to the option above it. -- If the "down" arrow key is pressed, the menu selection will move to the option below it. If "up" is pressed -- on the topmost option, or "down" is pressed on the bottommost option, the menu selection will loop to the other -- side of the selection menu. local function onKeyEvent(event) local key = event.keyName print(key) if (key == "down") then print("went down") currentSelect = currentSelect + 1 if (currentSelect \> 2) then currentSelect = 1 print("updated to top") end changeSelect(currentSelect) elseif (key == "up") then print("went up") currentSelect = currentSelect - 1 if (currentSelect \< 1) then currentSelect = 2 print("updated to bottom") end changeSelect(currentSelect) end print(currentSelect) return true end
This is using composer, in the part where you put the functions you want called multiple times. I call:
Runtime:addEventListener("key", onKeyEvent)
in the create portion of the code. The print commands are just to help me troubleshoot, and will be removed once I figure things out.
Okay, so this code is meant to move the cursor up or down (depending on which you choose) to scroll through different menu options, of which there are two right now. It should loop to the other end of the menu if it runs out of options.
Instead, it currently will move to the second option when I press up or down, and doesn’t move. The baffling part is what I found when I tried to troubleshoot:
- The only print command it ever goes through is the one that prints currentSelect
- print(currentSelect) prints 2. The value I set currentSelect to was 1, and this is the only method that updates it. It somehow manages to update it, and move the cursor one time, without ever printing any of the other commands.
- When I press a key, print(currentSelect) gets called twice, so two "2"s are printed.
- The left and right arrow keys both produce two "1"s in the console screen for some reason.
- None of this changes if I change key = event.keyName to key = event.keyPhase, for some reason.
So, I have no clue what’s going on. I honestly have no clue how it could possibly be getting the results it is. I’m willing to give the full code if asked; it’s just some code I made to learn how to do stuff, so I don’t mind.
Any help would be appreciated!