我正在使用eclipse并且我尝试了几个代码来使角色跳跃并且似乎没有任何效果,它只是取消了掉落和左右键,我不在乎用什么键来制作它跳,任何帮助都会很棒。
这是我的代码:
package com.firstgame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Ac extends JPanel implements Runnable {
public Rectangle floor;
public Rectangle character;
public int keyRight = KeyEvent.VK_RIGHT;
public int keyLeft = KeyEvent.VK_LEFT;
public int floorheight = 80;
public int fps = 1000;
public int characterheight = 36;
public int characterwidth = 24;
public int fallingFrame = 0;
public int fallingSpeed = 1;
public int movementFallingSpeed = 5;
public int movementResetSpeed = 1;
public int movementSpeed = 1;
public int movementFrame = 1;
public int noseHeight = 10;
public int noseWidth = 10;
public boolean objectDefine = false;
public boolean falling = false;
public boolean running = true;
public boolean left = false;
public boolean right = false;
public boolean leftNose = true;
public boolean rightNose = true;
public Thread game;
public Ac (Ad f) {
setBackground(Color.blue);
defineObjects();
game = new Thread(this);
game.start();
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == keyLeft) {
left = true;
rightNose = false;
leftNose = true;
}
if(e.getKeyCode() == keyRight) {
right = true;
leftNose = false;
rightNose = true;
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == keyLeft) {
left = false;
}
if(e.getKeyCode() == keyRight) {
right = false;
}
}
});
}
void defineObjects() {
character = new Rectangle ((Am.width/2) - (characterwidth/2), (Am.height/2) - (characterheight/2), characterwidth, characterheight);
floor = new Rectangle(0, 300, 600, 100);
objectDefine = true;
System.out.println("Shapes DefineObjects is running");
repaint();
}
public void paint(Graphics g) {
super.paint(g);
if (objectDefine) {
System.out.println("Graphics is running");
g.setColor(Color.CYAN);
g.fillRect(character.x, character.y, character.width, character.height);
g.setColor(Color.green);
g.fillRect(floor.x, floor.y, floor.width, floor.height);
if(leftNose) {
g.setColor(Color.yellow);
g.fillRect(character.x - noseWidth, character.y + (character.height/4), noseWidth, noseHeight);
} else if(rightNose) {
g.setColor(Color.yellow);
g.fillRect(character.x + character.width, character.y + (character.height/4), noseWidth, noseHeight);
}
}
}
public void run() {
while (running) {
//character feet
Point pt1 = new Point(character.x, character.y + character.height);
Point pt2 = new Point(character.x +character.width, character.y +character.height);
//Falling
if(fallingFrame >= fallingSpeed) {
if (floor.contains(pt1) || floor.contains(pt2)) {
falling = false;
} else {
character.y += 1;
falling = true;
}
if (falling) {
character.y += 1;
}
fallingFrame = 0;
}else {
fallingFrame += 1;
}
//movement speed check
if(falling) {
movementSpeed = movementFallingSpeed;
} else {
movementSpeed = movementResetSpeed;
}
//Movement
if(movementFrame >= movementSpeed) {
if(right) {
character.x += 1;
}
if(left) {
character.x -= 1;
}
movementFrame = -1;
} else {
movementFrame += 1;
}
fpsSetter();
repaint();
System.out.println("Run Method is running");
}
}
public void fpsSetter() {
try{
game.sleep(fps/1000);
}
catch(Exception e) {
e.printStackTrace();
System.out.println("fpsSetter is running");
}
}
}