我正在使用的Character类读取键码并进行移动 基于此。它跳跃并移动,但问题是它没有 正确地跳跃和移动。有没有办法解决 此
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Character
{
int c_posX, c_posY; // x and y positions
int c_velx; // velocity for x
public int gravity = 1, c_speedX = 20, c_diam = 25;
public int c_speedY = 26; // the initial jumping speed.
public Character(int c_posX, int c_posY)
{
this.c_posX = c_posX;
this.c_posY = c_posY;
c_velx = 0;
}
public int getCposX()
{
return c_posX;
}
// Create a method to check the Y coordinate of the player
public int getCposY()
{
return c_posY;
}
// Create a method to check the width of the player
public int getCdiam()
{
return c_diam;
}
public void updateCY() // if the character is jumping, then continue to add gravity
{
if (ifJumping() == true) {
c_speedY = c_speedY - gravity;
c_posY = c_posY - c_speedY;
}
}
public void keyPressed(int keycode)
{
if (keycode == 37) // if the user presses the left arrow
{
c_velx = -c_speedX;
}
if (keycode == 39)
{
c_velx = c_speedX; // if the user presses the right arrow
}
if (keycode == 38)
{
initialize(keycode); // if the user presses the up arrow,check if it can jump
}
}
public void updateCX() // update the x position of the character
{
c_posX += c_velx;
c_velx = 0;
}
public int returnKey(int keycode)
{
return keycode;
}
public void stopJumping() // stop jumping once the character reaches a certain velocity
{
if (DoneJumping() == true)
{
c_posY = 0;
}
}
public boolean ifJumping() // check if the character is jumping
{
if (c_speedY <= 25 & DoneJumping() == false) {
return true;
}
return false;
}
public boolean DoneJumping() // return true if the character's y velocity is less than -30.
{
if (c_speedY < (-30))
{
return true;
}
return false;
}
public void initialize(int keycode)
{
if (ifJumping() == false) // start jumping if it is not jumping
{
c_speedY = 25;
c_posY = c_posY - c_speedY;
}
}
}