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

2024/11/19

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

trulydevious trulydevious

2024/11/19

#
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

2024/11/20

#
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
}
trulydevious trulydevious

2024/11/21

#
Does this still work with the SmoothMover? Because of the size of my worlds (sizes themselves vary, but the cell size is 35x35), the character was moving too fast, so I had to use SmoothMover to fix that.
danpost danpost

2024/11/21

#
trulydevious wrote...
Does this still work with the SmoothMover? Because of the size of my worlds (sizes themselves vary, but the cell size is 35x35), the character was moving too fast, so I had to use SmoothMover to fix that.
The code should have no problems working with the SmoothMover class. What might be a problem, however, is using the SmoothMover class in a world where the cell size is not 1. The main reason for the SmoothMover class is to track the precise location of an actor in units smaller than the cell size of 1. I am guessing it could be used with larger cell sizes, but the actor will jump from cell to cell with larger cell sizes. Maybe I do not have enough info or just don't understand exactly what you are trying to do here. Or, maybe I am just a bit confused on how a SmoothMover might act in a world with large cell sizes. I just don't think it was intended to be used in a "grid" world. If I am not mistaken, I believe you can use a cell size of 1 and control your actor (probably without the SmoothMover class) such that it turns only at specific locations (where the center of the gridded cells would be).
trulydevious trulydevious

2024/11/22

#
I used the SmoothMover because I could not get my character to move slower any other way, and that's the only thing that worked for me
danpost danpost

2024/11/22

#
trulydevious wrote...
I used the SmoothMover because I could not get my character to move slower any other way, and that's the only thing that worked for me
You need to describe exactly what you want. Showing more code might help. In fact, your MyWorld code would have information in it that would remove some doubt as to what you want. My first concern is how the world is created.
You need to login to post a reply.