button press goes onto next screen
-
so I made a menu but I need a button press for it to work
-
Adding button controls is quite easy to begin with but quickly becomes complicated depending on what you want to do.
To get the state of the controls, you must call the controls() function in your main loop. Usually this happens at the start. Here is a basic template:
loop clear() c = controls(0) if c.a then // The A button is being pressed endif update() repeat
However - this will check if the A button is being pressed, and will be
true
for as long as the button is held. This means if we were to do something inside that if statement, it will keep happening until we let go. This sometimes causes problems. Let's say you wanted to increase a number by 1 every time you press the A button:num = 0 loop clear() c = controls(0) if c.a then num += 1 endif update() repeat
In the above program, it becomes almost impossible to actually increase the number by 1 - we would have to press the button for a single frame. This makes something like menu selection really hard to get working properly, so I recommend trying this method. It looks a little strange, but I did explain it in some detail in a Fuze Tutorial Video:
Essentially you want to store the state of the controls on the previous frame. You can do this with a variable easily:
c = controls(0) c_prev = c loop clear() c = controls(0) // This if statement means: "If A is pressed and it wasn't being pressed on the previous frame" - Basically, this will only happen once. If you hold A down, it can't pass through the IF anymore! if c.a and !c_prev.a then // A has been pressed - this will only happen once endif update() // Store the controls into the previous state variable before we start the next frame c_prev = c repeat
Hope this helps you!
-
To add to this, a great way of navigating a menu is through states, just variables you check for in every loop, drawing only what's necessary to get through the menus.
-
@AlexTheNewCoder Although it might be a little advanced for the things you are used to at the moment, @Devieus makes a good point and it gave me the motivation to make a menu system program which uses states. You can find it at this post:
https://fuzearena.com/forum/topic/1910/menu-system-program
Don't worry if it's confusing at first. It might be a fun experience to figure it out and try to add to it! If you can get your head around it, this sort of technique is unbelivably useful in many applications. I actually also touch on it in the video I linked before - but the states in that program are for the sprite animation. In this system, the states are for the current page of the menu we're looking at. As you can see - it's a versatile technique!