Walking sound on keyPressed

edited October 10 in Library Questions

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

Sign In or Register to comment.