We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I am working on a small game for a school project and i am running into a problem with the walking sound. I want it to loop on the walking key pressed and stop when released but im getting this horrible noise. I think its because i keep the key pressed and it loops on itself at a high rate. Can anyone help me?
My Code:
import processing.sound.*;
SoundFile walk;
SoundFile music;
boolean[] keys = new boolean[128];
float x, y;
PVector speed;
int bodySize;
float timer;
void setup() {
size(1000, 600);
x = width/2;
y = height/2;
bodySize = 50;
walk = new SoundFile(this, "walk.wav");
music = new SoundFile(this, "music.mp3");
}
void draw() {
background(0);
move();
ellipse(x, y, bodySize, bodySize);
}
void move() {
if (keys['a']) {
x = x-4;
}
if (keys['w']) {
y = y-4;
}
if (keys['s']) {
y = y+4;
}
if (keys['d']) {
x = x+4;
}
}
void keyPressed() {
keys[key] = true;
walk.play();
}
void keyReleased() {
keys[key] = false;
}
Answers
Your keyPressed is restarting the walk noise 30 times a second.
Perhaps the logic should be something like "if walk isn't already playing, play" -- and the keyReleased should be "stop playing walk". I also don't see you actually setting loop().
One possible approach,
Kf