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

2017/6/3

isKeyDown single action.

mrlemon mrlemon

2017/6/3

#
I have an actor in a little kungfu game who can kick, punch, jump, jumpkick, crouch, crouchkick and crouchpunch. These actions should be performed once by the push of a button. I.E. when "a" is pressed the actor should kick once. However, when "a" is pressed the actor keeps kicking. I have been playing around with booleans but can't seem to figure this out. The action itself is basically just a change of image. Can anyone help me out please? Here's the base i've been using code for a kick:
if(Greenfoot.isKeyDown("a") && facingLeft==true) //left kick
        { 

            if (getImage().equals(image3) || getImage().equals(image1) || getImage().equals(image2)) {
                
                setImage(image9); // wijzig afbeelding naar afbeelding 9
                Greenfoot.playSound("kick.wav");
                
            }


            else { // afbeelding 2 wordt getoond
                setImage(image1); // wijzig afbeelding naar afbeelding 1

            }
        }
danpost danpost

2017/6/3

#
mrlemon wrote...
I have an actor in a little kungfu game who can kick, punch, jump, jumpkick, crouch, crouchkick and crouchpunch. These actions should be performed once by the push of a button. I.E. when "a" is pressed the actor should kick once. However, when "a" is pressed the actor keeps kicking
You just need to keep track of what state the 'a' key is in. Add a boolean field:
private boolean aDown;
When its value is not that of what 'isKeyDown("a")' returns, then the state of the key was changed. Change the value of the Boolean field, test the new value to see if it was pressed and start the action if it was. It would be something like this:
if (aDown != Greenfoot.isKeyDown("a"))
{
    aDown = ! aDown;
    if (aDown)
    {
        setImage(facingLeft ? image9 : imageR9);
        Greenfoot.playSound("kick.wav");
    }
    else setImage(facingLeft ? image1 : imageR1);
}
The names of the image references (fields names) will probably need adjusted.
mrlemon mrlemon

2017/6/3

#
I do not understand a single piece of that code. I'm sorry, very new to this. Could you clarify a bit please. On line one you seem to be comparing a boolean to an isKeyDown? Or is that just another notation for a true/false value?
danpost danpost

2017/6/3

#
mrlemon wrote...
I do not understand a single piece of that code. I'm sorry, very new to this. Could you clarify a bit please. On line one you seem to be comparing a boolean to an isKeyDown? Or is that just another notation for a true/false value?
Line one compares two boolean values. 'aDown' is a boolean field and 'Greenfoot.isKeyDown("a")' returns a boolean value. The use of '!=' asks if the two booleans are not the same value. If they are not, since 'aDown' is holding the last state of the "a" key, the state of the key must have just changed. So, line 3 changes the value of the boolean field to match the new state of the key and lines 4 through 9 performs the action(s) required for that new state.
mrlemon mrlemon

2017/6/5

#
Okay, so that made a lot more sense to a newbie like me. I've implimented this in my code and works fine now. However the code for jumping needs some further adjustment. I have to impliment a delay for the player to jump but this has effect on other actors too. If i do not use the delay the player will not jump but just change images. Any ideas? See code below:
if(upDown != Greenfoot.isKeyDown("up") && facingLeft == true) //jump facing left
        {   
            upDown = !upDown;
            if(upDown)
            {
            setImage(image13);
            int x = getX();
            for(int y = getY(); y>=365; y--)
            {
                setLocation(x, y); 
            }
            Greenfoot.delay(3);
            }
        }
Super_Hippo Super_Hippo

2017/6/5

#
You should not use the 'delay' method (because as you saw, it delays the whole scenario). You should also not use a loop there. You will need a vertical speed int (let's call it 'vSpeed'). When you jump, you set vSpeed to some negative value (higher value = higher jump). Whenever the actor is in the air (not within the jump condition), vSpeed is affected by gravity (= a constant → vSpeed increases linearly). Then you let the actor fall by the vSpeed value:
//for example gravity=1 -- on earth this is about 9,81 m/s^2 -- you have to test what number fits for your scenario
vSpeed = vSpeed + 1;
setLocation(getX(), getY() + vSpeed);
After that, it should be checked that the actor did not fall into the ground and jumping should only be allowed when the actor stands on the ground (= is not in the air).
danpost danpost

2017/6/5

#
The 'delay' method of the Greenfoot class suspends the execution for the specified number of act steps (like a temporary pausing of the scenario). Jumping is the rising and falling of an object over several act steps. You need to code it for what needs done during any one individual act step. How you implement the coding is up to you (whether you want to limit the height and just control the direction, like floating up and down, or you want to make it more realistic by applying a downward acceleration to the vertical speed, for the effect of gravity. Either way, or whichever way you choose, in each act step, you need to determine the direction and speed to move depending on the current state of the actor.
You need to login to post a reply.