Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Attempting to Make a Menu Selection Screen and Getting Some Baffling Results
Started by saramwinstanley Sep 28 2018 09:13 PM

1 reply to this topic
key event menu
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 4 posts
  • Corona SDK

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)

-- 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
	if (key == "down") then
		print("went down")
		currentSelect = currentSelect + 1
		if (currentSelect > 2) then
			currentSelect = 1
			print("updated to top")
	elseif (key == "up") then
		print("went up")
		currentSelect = currentSelect - 1
		if (currentSelect < 1) then
			currentSelect = 2
			print("updated to bottom")
	return true

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!

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 4 posts
  • Corona SDK

Update: I managed to figure it out! The event listener calls the function twice (once when the key is pressed, once when it's released). I actually thought this might be the case, but when I tried to account for it, I used event.keyPhase, which gives the same print output as event.phase but doesn't work in if statements, for some reason. Having the function called twice was the main thing causing issues, but that made it hard to troubleshoot.