Oops. Submitted too soon. Gimme a sec!
Okay. Here were some problems you had:
When evaluating a condition in an "if" statement
- Code: Select all
if (offsetpause == 1) {
you must be sure to use "==" and not "=". In programming, those are two very different operators. A single equals sign is an "assignment". It says, whatever the value on the right is, store that inside the left. So saying "offsetpause = 0" will set offsetpause to 0. I'm sure you knew that part. But when you say "offsetpause == 1", that statement will either say "true" if offsetpause equals 1, or "false" otherwise. By saying
- Code: Select all
if (offsetpause = 1) {
what you're actually doing is assigning 1 to offsetpause, and then the if statement evaluates to true (because the assignment was successful).
Next problem. You aren't matching your brackets how you think you are. Your if-else structure isn't quite right.
- Code: Select all
else{
if(offsetpause = 0){
offsetpause = 1;
}
You're opening a new bracket inside the else, when I think you meant to write "else if (offsetpause == 0) {"
Rodot's way of using the while loops and multiple gb.update() calls will work, but I prefer only having one. In my opinion, you should instead use a variable, like "int state" and set it to 0. Then, in place of your "while" loops, you just have "if (state == 0)" for the first screen and "if (state == 1)" for the next screen, and so on. That way you have a lot of flexibility, because at any time you could set the state to whatever you want to show a different screen rather than going through the while loops in order.
Try this out:
- Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
int state;
int num;
int offsetpause;
// the setup routine runs once when Gamebuino starts up
void setup() {
restart_game();
// initialize the Gamebuino object
gb.begin();
//display the main menu:
gb.titleScreen(F("jasons HEX thingy"));
gb.titleScreen(F("press A to start"));
}
void restart_game() {
state = 0;
num = 0;
offsetpause = 0;
}
// the loop routine runs over and over again forever
void loop() {
if (gb.update()) {
//first screen
if (state == 0) {
//prints Hello World! on the screen
gb.display.println(F("Hello World!"));
//me stuffz
gb.display.println("Jason Harriot 2015");
if (gb.buttons.pressed(BTN_A)){
state = 1;
}
}
//second screen
if (state == 1) {
if (gb.buttons.pressed(BTN_A)) {
if (offsetpause == 1) {
offsetpause = 0;
num += 1;
} else if (offsetpause == 0) {
offsetpause = 1;
}
}
gb.display.println(num);
if (gb.buttons.pressed(BTN_B)) {
num = 0;
gb.popup(F("reset"), 10);
}
}
if (gb.buttons.pressed(BTN_C)) {
// Go back to the title screen if C is pressed in the 2nd screen
restart_game(); // Be sure to reset the game variables
gb.titleScreen(F("super dumb app"));
}
}
}
You can see I moved all the variables that are needed to the top of the file. I set their initial value in a function called "restart_game()", which I called in Setup (which is called once on power-on). I called restart_game() later (if you press C) in order to reset the game. Notice how the code that check if I press "c" is outside of the "first screen" and "second screen" if statements? That way, no matter which screen I am on, the C button will always take me back to the start.