Help with gb.sound?

By Party_Pete, 2 weeks ago

Hello Gamebuinians! I'm having a problem with sound on the gamebuino, where if I use the below code segment, gb.sound.playTick() will execute multiple times for up to a second after a button is released, when it's only tied to a gb.buttons.pressed() conditional. I've also had problems with menu sounds repeating themselves indefinitely after being pressed, or until the button is pressed again. Thanks to whoever can shed some light on this issue!

#include <Gamebuino-Meta.h>

class Manager {
    public:
      void doSomething();
};

void Manager::doSomething(){
  gb.sound.playTick();
}

const char* mainMenuEntries[] = {
  "Story Mode",
};

void setup() {
  gb.begin();
  gb.display.setTextWrap(true);
  gb.gui.menu("Super Buino Ware",mainMenuEntries);
}

void loop() {

  Manager helper;
  //intro screen
  while(!gb.update());
  gb.display.clear();

  gb.display.setColor(DARKBLUE);
  gb.display.fillRoundRect(0, 0, gb.display.width(), gb.display.height(), 6);
  gb.display.setColor(WHITE);
  gb.display.println("Welcome to Super Buino Ware! You are in a game show set in a 10 floor tower! Each floor has a challenge to win! You lose if you fail two! Get to the top of the tower to win $1000000!");

  if (gb.buttons.pressed(BUTTON_A)) {
    helper.doSomething();
  }
}

Last comments

joeroot

NEW 5 days ago

Message deleted

Party_Pete

NEW 2 weeks ago

steph steph

Thanks so much for your help! I will use the code you gave me to learn all about programming on the Gamebuino. And yes, I will use the discussion box next time :)

steph

NEW 2 weeks ago

Sorry, I didn't see your request last night... but I'm surprised no one answered you! Here is a proposal to improve your code. I suppose you'll be able to understand it with my comments. If you don't understand some of the details, don't hesitate to come back to us with new questions.

#include <Gamebuino-Meta.h>

const uint8_t SCREEN_WIDTH = gb.display.width();
const uint8_t SCREEN_HEIGHT = gb.display.height();

const uint8_t GAME_MODE_STAND_BY = 0;
const uint8_t GAME_MODE_INTRO    = 1;

class Manager
{
  public:
    void doSomething();
};

void Manager::doSomething()
{
    gb.sound.playTick();
}

const char *mainMenuEntries[] = {
    "Story Mode",
};

// instantiation should only be done once here,
// at initialization
Manager helper;

// define the default game mode
uint8_t gameMode = GAME_MODE_INTRO;

void setup()
{
    gb.begin();
    gb.display.setTextWrap(true);
    menu();
}

void loop()
{
    // don't create a new instance
    // with each cycle of the loop!
    // (look above)
    // Manager helper;

    while (!gb.update());
    // only clear the screen
    // when you need it!
    // gb.display.clear();

    if (gb.buttons.pressed(BUTTON_A)) {
        helper.doSomething();
    } else if (gb.buttons.pressed(BUTTON_MENU)) {
        menu();
    }

    switch (gameMode) {
        case GAME_MODE_INTRO:
            intro();
            break;
    }
}

void menu() {
    uint8_t selected = gb.gui.menu("Super Buino Ware", mainMenuEntries);
    switch (selected) {
        case 0:
            gameMode = GAME_MODE_INTRO;
            break;
        default:
            gameMode = GAME_MODE_STAND_BY;
    }
}

void intro()
{
    // here you need to clear the screen ;-)
    gb.display.clear();

    gb.display.setColor(DARKBLUE);
    gb.display.fillRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 6);
    gb.display.setColor(WHITE);
    gb.display.println("Welcome to Super Buino Ware! You are in a game show set in a 10 floor tower! Each floor has a challenge to win! You lose if you fail two! Get to the top of the tower to win $1000000!");

    // change the game mode to avoid looping
    // on this function!
    gameMode = GAME_MODE_STAND_BY;
}

Besides, you didn't post your message in the right section! You made it a Creation.... This section is reserved for community creations (games, utilities, tutorials, etc.). Instead, you should have asked your question on the forum, in the Community section. Think about it in the future ;-)

Party_Pete

2 weeks ago

Thanks so much for your help! I will use the code you gave me to learn all about programming on the Gamebuino. And yes, I will use the discussion box next time :)