This site requires JavaScript, please enable it in your browser!
Greenfoot back
trulydevious
trulydevious wrote ...

2 days ago

Playing a sound when a key is pressed, then stopping it when the key is released?

trulydevious trulydevious

2 days ago

#
I want to play a sound when my player is walking, this is the code I have for the checkMovement method. How do I make it to where the walking sound plays when I press w or a or s or d and then stops when I release the keys?
private void checkMovement() {
        // Gets the exact location.
        double x = getExactX();
        double y = getExactY();

        // Stores the original location so that the player can stay in that location whenever they try to go through an object that they aren't supposed to.
        double originalX = x;
        double originalY = y;

        GreenfootSound walking = new GreenfootSound("walking.mp3");

        // Move up whenever the "w" key is pressed.
        if (Greenfoot.isKeyDown("w")) {
            y -= speed;
        }

        // Move down whenever the "s" key is pressed.
        if (Greenfoot.isKeyDown("s")) {
            y += speed;
        }

        // Move left whenever the "a" key is pressed, while also flipping the player to face the left.
        if (Greenfoot.isKeyDown("a")) {
            x -= speed;
            if (facingRight) {
                flipImage();
                facingRight = false;
            }
        }

        // Move right whenever the "d" key is pressed, while also flipping the player to face the right.
        if (Greenfoot.isKeyDown("d")) {
            x += speed;
            if (!facingRight) {
                flipImage();
                facingRight = true;
            }
        }

        // Temporarily move to the new location to test for collisions.
        setLocation(x, y);

        // Return to the original position if colliding with a Tree.
        if (isTouching(Tree.class)) {
            setLocation(originalX, originalY);
        }

        // Return to the original position if colliding with a Barrier.
        if (isTouching(Barrier.class)) {
            setLocation(originalX, originalY);
        }
    }
danpost danpost

yesterday

#
trulydevious wrote...
I want to play a sound when my player is walking, this is the code I have for the checkMovement method. How do I make it to where the walking sound plays when I press w or a or s or d and then stops when I release the keys? << Code Omitted >>
Line 10 needs to outside the block (in the class itself, not in a method or constructor). I prefer the following to begin 4-way movement by key press:
int dx = 0, dy = 0; // movement direction (by key press)
if (Greenfoot.isKeyDown("w")) dy--;
if (Greenfoot.isKeyDown("a")) dx--;
if (Greenfoot.isKeyDown("s")) dy++;
if (Greenfoot.isKeyDown("d")) dx++;
if (dx*dy != 0 || dx+dy == 0) return;
The last line cancels movement if (a) direction is undetermined or (b) no direction was given. The first part will fail if both dx and dy are non-zero (pass if either is zero); the second part will fail (remember one is definitely zero now) if both are zero, which can happen if no key is pressed or the keys pressed cancel each other, like "a" and "s", or "w" and "d", or both. For you, the last line might be expanded on:
if (dx*dy != 0 || dx+dy == 0) {
    walking.pause();
    return;
}
Then you could continue with:
walking.playLoop();
setLocation(getExactX()+speed*dx, getExactY()+speed*dy);
if (isTouching(Tree.class || isTouching(Barrier.class) {
    setLocation(getExactX-speed*dx, getExactY()-speed*dy);
}
if (dx != 0)  { // on horizontal movement attempt
    setImage(images[(1+dx)/2]); // using an array of length 2 for the images with left facing first
}
You need to login to post a reply.