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

2017/6/20

Help with Jumping in my game

1
2
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/20

#
I need help with making my dog jump in a game, but I don't know how to make him move up and I don't how to make it delay before falling. Here is my code:
    /**
     * Move - this allows the dog to move.
     */
    public void move()
    {
        if (Greenfoot.isKeyDown("W"))
        {
             
        }
        if (Greenfoot.isKeyDown("D"))
        {
            setImage("Unknown-7.png") ;
            move(3) ;
        }
        if (Greenfoot.isKeyDown("A"))
        {
            setImage("Unknown-5.png") ;
            move(-3) ;
        }
    }
I need to make it in lines 6 through 9. The rest works properly, though.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/21

#
Its been 4 hours, and no replies. Can someone please help?
danpost danpost

2017/6/21

#
TheAwesomePuppy404 wrote...
I need to make it in lines 6 through 9.
The only thing that can be done among those lines is initiating a jump. You will need additional code for moving vertically (rising and falling) and a field to track the speed along the vertical. There is also a secondary condition for initiating a jump -- that of "standing" on something or being at ground level. Then, there is the constant incremental adding to the vertical speed that will represent the constant pull of gravity. There are also location and speed adjustments that will need to be done when hitting an obstacle while rising or falling.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
Ok, but I'm not sure what code would move it vertically. I also need to know how to make items solid.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
I actually changed my code to:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class dog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class dog extends Actor
{
    /**
     * Act - do whatever the dog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move() ;
        jump() ;
        touch() ;
    }
    
    /**
     * Move - this allows the dog to move.
     */
    public void move()
    {
        if (Greenfoot.isKeyDown("D"))
        {
            setImage("Unknown-7.png") ;
            move(3) ;
        }
        if (Greenfoot.isKeyDown("A"))
        {
            setImage("Unknown-5.png") ;
            move(-3) ;
        }
    }
    
    /**
     * Jump - this lets the dog jump.
     */
    public void jump()
    {
        if (Greenfoot.isKeyDown("W"))
        {
             
        }
    }
    
    /**
     * Touch - this allows the dog to know what he is touching at the 
     * given moment.
     */
    public void touch()
    {
        if (isTouching(spikes.class))
        {
            setLocation(62,370) ;
        }
    }
}
Now it needs to be in 44 to 46.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
I also want have the dog be able to move forward and backward while jumping.
danpost danpost

2017/6/22

#
TheAwesomePuppy404 wrote...
I also want have the dog be able to move forward and backward while jumping.
As you have already separated vertical movement from the horizontal movement, this is taken care of. You could rename the 'move' method to 'moveHorizontally' and the 'jump' method to 'moveVertically' to make the names more appropriate. You are asking for a lot here without much to start with. I have started a tutorial on jumping, but have only the basics. I will add what I have to my tutorial site and provide a link once it is up.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
But how do you make items solid?
Yehuda Yehuda

2017/6/22

#
Take your problems one at a time. Now your working on jumping, once you're done with that you can make sure that when you hit something you're bounced back.
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
And how do you make variables in greenfoot?
Yehuda Yehuda

2017/6/22

#
TheAwesomePuppy404 wrote...
And how do you make variables in greenfoot?
The same way it's done anywhere else. Greenfoot is regular Java just with added methods that have to do with using JPanel Graphics and playing sounds. To create an int variable for vertical speed you would do the following.
private int vSpeed;
//and some other fields (static)
private static int JUMP_STRENGTH = 15, GRAVITY = 1;
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
But how you create any old variable (for example the value of x)?
Yehuda Yehuda

2017/6/22

#
TheAwesomePuppy404 wrote...
But how you create any old variable (for example the value of x)?
I just told you how to create a variable. I'm not sure what the difference is between what I showed above and "any old variable (for example the value of x)".
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/22

#
What is a static field?
Yehuda Yehuda

2017/6/22

#
TheAwesomePuppy404 wrote...
What is a static field?
A static field is a variable that can be changed in code but the value will only change itself during compilation. It won't reset with each run of the program.
There are more replies on the next page.
1
2