Walking Sfx Help


(Nate ) #1

Hey guys! I started a little fun Halloween themed game last night and it is a nice change of pace from my current game project. I have a character who walks around a world, this is my control code, don’t make fun of me:

//player controls
		if (Input.check(Key.LEFT) && !Input.check(Key.RIGHT) && !Input.check(Key.DOWN) && !Input.check(Key.UP))
		{
			xSpeed -= power;
			pressed = true;
			playerSprite.play("left");
			direction = 1;
		}
		if (Input.check(Key.RIGHT) && !Input.check(Key.LEFT) && !Input.check(Key.DOWN) && !Input.check(Key.UP))
		{
			xSpeed += power;
			pressed = true;
			playerSprite.play("right");
			direction = 2;
		}
		if (Input.check(Key.UP) && !Input.check(Key.DOWN) && !Input.check(Key.LEFT) && !Input.check(Key.RIGHT))
		{
			ySpeed -= power;
			pressed = true;
			playerSprite.play("up");
			direction = 3;	
		}
		if (Input.check(Key.DOWN) && !Input.check(Key.UP) && !Input.check(Key.LEFT) && !Input.check(Key.RIGHT))
		{
			ySpeed += power;
			pressed = true;
			playerSprite.play("down");
			direction = 4;
		}
		//end of control code

This is how I play the footsteps sounds:

	if (Input.pressed(Key.LEFT) || Input.pressed(Key.RIGHT) || Input.pressed(Key.UP) || Input.pressed(Key.DOWN))
	{
		playSound();
	}

This is the playSound method:

		private function playSound():void
	{
		sfxWalk.loop(.5);
	}

It works without playing the sounds ten million times and being all glitchy. The issue I am having is if say I am walking to the left, the the left key is check and pressed, then I press another arrow key while holding the left key, then release the left key, the character will move in the new direction but there will be no sound. I know this is a fundamental issue, but I was hoping to get some solid guidance so my walking and sounds for games to come is not so finicky.

Thanks in advance all from Flashpunk! :smiley:


(JP Mortiboys) #2

The issue is quite simply that you’re only playing a sound if one of the movement keys has been pressed that frame; you’re not checking if one has been released but you’re still moving… so try this:

// somewhere else, main init
Input.define('MOVEMENT', Key.LEFT, Key.RIGHT, Key.UP, Key.DOWN);

// remplacement code
	if (Input.pressed('MOVEMENT') || (Input.released('MOVEMENT') && pressed))
	{
		playSound();
	}

That should do the trick - although I’d have to see your logic for stopping the looping sound as well to be sure.


(Nate ) #3

Thank you so much! That worked perfectly, I have been working with FlashPunk since July and was just made aware of Input.define… that could have saved me many headaches in the past.

Thanks again NotARaptor!