Help with gb.sound?

By Party_Pete, 8 months 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

avatar
gamebuino-admin
8 months ago
Author :  Party_Pete

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 :)

avatar
gamebuino-admin
8 months ago
Author :  Steph

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;

<span class="hljs-keyword">while</span> (!gb.update());
<span class="hljs-comment">// only clear the screen</span>
<span class="hljs-comment">// when you need it!</span>
<span class="hljs-comment">// gb.display.clear();</span>

<span class="hljs-keyword">if</span> (gb.buttons.pressed(BUTTON_A)) {
    helper.doSomething();
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (gb.buttons.pressed(BUTTON_MENU)) {
    menu();
}

<span class="hljs-keyword">switch</span> (gameMode) {
    <span class="hljs-keyword">case</span> GAME_MODE_INTRO:
        intro();
        <span class="hljs-keyword">break</span>;
}

}

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 :wink:
gb.display.clear();

gb.display.setColor(DARKBLUE);
gb.display.fillRoundRect(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, SCREEN_WIDTH, SCREEN_HEIGHT, <span class="hljs-number">6</span>);
gb.display.setColor(WHITE);
gb.display.println(<span class="hljs-string">&quot;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!&quot;</span>);

<span class="hljs-comment">// change the game mode to avoid looping</span>
<span class="hljs-comment">// on this function!</span>
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 ;-)