0

I am making a 2d fighting game (think smash bros) for school and I can't figure out how to make the sprite jump using velocity for y. Here is my KeyPressed code:

if (key == KeyEvent.VK_LEFT)
    {
        p2.setVelX(-3);
    }

    else if (key == KeyEvent.VK_RIGHT) {
        p2.setVelX(3);
   }

    else if (key == KeyEvent.VK_UP) {
        p2.setVelY(-3);
    }        

    else if (key == KeyEvent.VK_DOWN)
    {
        p2.setVelY(3);
    }

and here is some from Player:

public class Player {
private double x; 
private double y;
private BufferedImage player;

private double velX = 0;
private double velY = 0;
public Player(double x, double y, Game game) {
    this.x = x;
    this.y = y;
    SpriteSheet ss =  new SpriteSheet(game.getSpriteSheet());
    player = ss.grabImage(1,1,32,32);
}

private void initPlayer() {

}

public void tick()
{
    x+=velX;
    y+=velY;

    if(x <= 0)
        x=0;

    if (x >= 940-32)
        x=940-32;

    if(y <= 0)
        y=0;

    if (y >= 370-64)
        y=370-64;
}

public void render(Graphics g)
{
    g.drawImage(resize(player,64,64), (int)x, (int)y, null);
}

public double getX() {
    return x;
}

public double getY() {
    return y;
}

public void setX(double x)
{
    this.x = x;
}

public void setY(double y)
{
    this.y = y;
}

public void setVelX(double velX)
{
    this.velX = velX;
}

public void setVelY(double velY)
{
    this.velY = velY;
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();

    return dimg;
} 

}

What do I put for the VK_UP if() statement. Do I have to use swing and KeyStroke instead of a key listener? Thanks.

Brandyn
  • 11
  • 2
  • 1
    `FloatSpring`, cited [here](http://stackoverflow.com/q/11228554/230513), is an example. – trashgod Jan 25 '17 at 06:55
  • 1
    For [example](http://stackoverflow.com/questions/16493809/how-to-make-sprite-jump-in-java/16494178#16494178) – MadProgrammer Jan 25 '17 at 07:35
  • 1
    A concept you need to have is the idea that the object has an initial velocity, which over time decreases back to a known state (like 0), for [example](http://stackoverflow.com/questions/15897947/problems-with-javas-paint-method-ridiculous-refresh-velocity/15900205#15900205), for [example](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets-progressively-less-high-in-java/19626396#19626396) – MadProgrammer Jan 25 '17 at 07:40

0 Answers0