Skip to content
bbbbbr edited this page Apr 7, 2024 · 2 revisions

If you want to add sound to your game I recommend downloading the fixed version of the Sound Sample here

scr1 scr2 scr3 scr4 scr5

Pressing A + select will output the current register values, then you just need to call PlayFx with the values you see there.

If you don't have music on your game then you need to init register 5 (take a look here). Usually adding the next lines at the end of your method Start_STATE_GAME in StateGame.c will work for you

void START() {
	...
	
	NR52_REG = 0x80; //Enables sound, you should always setup this first
	NR51_REG = 0xFF; //Enables all channels (left and right)
	NR50_REG = 0x77; //Max volume
}

You can add this sound when the enemy changes its direction on SpriteEnemy.c

#include "Sound.h"
...
void UPDATE() {
	CUSTOM_DATA* data = (CUSTOM_DATA*)THIS->custom_data;
	if(TranslateSprite(THIS, 0, data->vy)) {
		data->vy = -data->vy;
		PlayFx(CHANNEL_4, 4, 0x0c, 0x41, 0x30, 0xc0);
	}
}

and this one when an enemy kills the player on SpritePlayer.c

#include "Sound.h"
...
void Update_SpritePlayer() {
	...
	SPRITEMANAGER_ITERATE(i, spr) {
		if(spr->type == SpriteEnemy) {
			if(CheckCollision(THIS, spr)) {
				PlayFx(CHANNEL_1, 10, 0x4f, 0xc7, 0xf3, 0x73, 0x86);
				SetState(StateGame);
			}
		}
	}
	...
}

don't forget to include Sound.h in both files

If you are playing an fx at the same time the music is playing and the music is using that channel then you need to mute it for a moment (the second parameter in PlayFx is the number of frames you want that music channel to be muted)

Clone this wiki locally