2017-01-20 16:37:14

Hi, everyone.
I have made a little script in which two characters  are fighting each other.
I've used an array to be able to play more random weapon sounds.
The problem is that when I press space, after two or three times, I get this error and I don't have any clue what's going on.
I'll put the relevant parts of the code below
I define this array globally
sound[] laser(3);
And next, somewhere in a function I have this
for(int s=0; s<laser.length(); s++)
{
laser[s].load("Sounds/Laser Gun"+s+".ogg");
}

And then, I have a while with an if,  where happens the following:

if space is pressed:
the enemy health decreases
then we have the laser_sound function
then is the enemy_attack function, and after that everything repeats until one of the two characters win the game.
Bellow you have the  laser_gun function
void laser_sound()
{
int r=random(0, laser.length());
laser[r].play();
}
so, what do you think the problem might be for getting this error?

2017-01-20 19:35:58

you must do this in your enemy function:

int r=random(0, laser.length()-1);

length() is the length of array, but array starts from0, length()-1 is the last element in array

2017-01-20 23:40:42 (edited by stefan_ilioaica 2017-01-20 23:43:34)

I've put the code in my enemy function, but I'm still having the same issue
PS: I put -1 after length in my laser_sound function, and it seems to work

2017-01-22 11:30:15

Stefan, that's what post 2 should have meant, and maybe has actually meant. BTW, you may want to consider using a sound pool instead of an array of sound objects. That's no longer necessary (for ages) and is not nearly as convenient. Just to let you know.

Lukas

I won't be using this account any more or participating in the forum activity through other childish means like creating an alternate account. I've asked for the account to be removed but I'm not sure if that's actually technically possible here. Just writing this for people to know that I won't be replying, posting new topics or checking private messages until the account is potentially removed.