Sound of stairs (SOLVED)

Started by Kinoko, Mon 04/07/2005 03:55:20

Previous topic - Next topic

Kinoko

I'm trying to write a script so that a certain sound plays as a character walks up stairs. I have a region set up to go on and off as you walk along, basically:



What I tried first is to put in the region interaction for when player steps onto region, simply PlaySound(9);
This didn't work very well, and the sound only went off every now and again and certainly not on every step. I tried thickening the lines, thinning them, and the result didn't vary much. I realise this is because the characters speed doesn't allow him to "walk" on every pixel, (his speed is 6, by the way) so I tried putting this code:

Code: ags

int reg=1;

if (((Region.GetAtRoomXY(player.x, player.y)) == region[2]) && (reg==1)) {
    reg=0;
    PlaySound(9);
}

if (((Region.GetAtRoomXY(player.x, player.y)) != region[2]) && (reg==0)) {
    reg=1;
}


in the room's repeatedly_execute section, and now if I stand on a region, the sound plays continuously. I also tried ==region[0] but it had the same result. I can understand why the first method didn't work, but what's going on with this code?

Does anyone have any other way I could do this?

Gilbert

Because you put int reg=1; into repeatedly_execute(), so it's reinitialized to 1 every game loop, put it outside of the function, on top of the script.

Kinoko

#2
Dargh -_- Thanks gil.

EDIT: Well, as expected, I get the same result with this method.

Gilbert

Hmmm odd, stick a Display() line into the != part and see if the reg=1 is run nearly every game loop when it's not desired.

Kinoko

It's definitely only turning on and off a few times in each walk over the stairs (halves if I 'run', not surprisingly).

I know the problem is that it's only counting the x/y coords that my character is standing on each game loop, and he moves too quick to be positioned on each coordinate. I don't have a  clue what to do about it though. I can't make the steps bigger.

Kweepa

Gil, I presume the "same result" is the result from the "steps onto region" method, i.e., it only plays the sound occasionally.

Here's what I'd do...
Just block out the whole stairs as region 2.
Then:

int lastStepSoundY = 0;
int stepIntervalY = 9;

repeatedly_execute_always

if (Region.GetAtRoomXY(player.x, player.y) == region[REGION_STAIR])
{
Ã, // round y to the nearest interval
Ã, int thisStepSoundY = player.y/stepIntervalY;
Ã, thisStepSoundY = stepIntervalY*thisStepSoundY;
Ã, // if it changes interval, play the sound
Ã, if (thisStepSoundY != lastStepSoundY)
Ã, {
Ã,  Ã, PlaySound(SOUND_CREAK);
Ã,  Ã, lastStepSoundY = thisStepSoundY;
Ã, }
}

Alternatively, if you want the sound to at the same rate when walking diagonally on the stairs...

int lastStepX = 0;
int lastStepY = 0;

int stepInterval = 9;

repeatedly_execute_always

if (Region.GetAtRoomXY(player.x, player.y) == region[REGION_STAIR])
{
Ã, int dx = lastStepX - player.x;
Ã, int dy = lastStepY - player.y;
Ã, if ((dx*dx + dy*dy) < stepInterval*stepInterval)
Ã, {
Ã,  Ã, PlaySound(SOUND_CREAK);
Ã,  Ã, lastStepX = player.x;
Ã,  Ã, lastStepY = player.y;
Ã, }
}

These aren't entirely equivalent - the first method might produce an irregular rhythm of sounds, depending on the speed of the character. You can adapt the second to the first (only Y) though.

Cheers,
Steve
Still waiting for Purity of the Surf II

Kinoko

That works pretty well Steve, thanks!

SMF spam blocked by CleanTalk