我的测试游戏中的精灵在按下我所拥有的关键输入时以极高的速度飞离屏幕,我不知道为什么。
KeyInput.java:
package com.cosmicluck.miner.framework;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import com.cosmicluck.miner.window.Handler;
public class KeyInput extends KeyAdapter
{
Handler handler;
public KeyInput(Handler handler)
{
this.handler = handler;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
for(int i = 0; i < handler.object.size(); i++)
{
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Player)
{
if(key == KeyEvent.VK_D) tempObject.setVelX(5);
if(key == KeyEvent.VK_A) tempObject.setVelX(-5);
}
}
if(key == KeyEvent.VK_ESCAPE)
{
System.exit(1);
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
for(int i = 0; i < handler.object.size(); i++)
{
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ObjectId.Player)
{
if(key == KeyEvent.VK_D) tempObject.setVelX(0);
if(key == KeyEvent.VK_A) tempObject.setVelX(0);
}
}
}
}
将速度更改为除0
keyPressed
以外的任何内容,使对象飞离屏幕。
这是我的Player.java:
package com.cosmicluck.miner.objects;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.LinkedList;
import com.cosmicluc.miner.framework.GameObject;
import com.cosmicluc.miner.framework.ObjectId;
public class Player extends GameObject
{
private float width = 48, height = 96;
private float gravity = 0.5f;
private final float MAX_SPEED = 10;
public Player(float x, float y, ObjectId id) {
super(x, y, id);
}
public void tick(LinkedList<GameObject> object) {
x += velX;
y += velY;
if(falling || jumping)
{
velY += gravity;
if(velY > MAX_SPEED)
velY = MAX_SPEED;
}
}
public void render(Graphics g)
{
g.setColor(Color.BLUE);
g.fillRect((int)x, (int)y, (int)width, (int)height);
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.RED);
g2d.draw(getBounds());
g2d.draw(getBoundsRight());
g2d.draw(getBoundsLeft());
g2d.draw(getBoundsTop());
}
public Rectangle getBounds() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int) ((int)y+(height/2)), (int)width/2, (int)height/2);
}
public Rectangle getBoundsTop() {
return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int)y, (int)width/2, (int)height/2);
}
public Rectangle getBoundsRight() {
return new Rectangle((int) ((int)x+width-5), (int)y+5, (int)5, (int)height-10);
}
public Rectangle getBoundsLeft() {
return new Rectangle((int)x, (int)y+5, (int)5, (int)height-10);
}
}
GameObject.java:
package com.cosmicluck.miner.framework;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.LinkedList;
public abstract class GameObject
{
protected float x, y;
protected ObjectId id;
protected float velX = 0, velY = 0;
protected boolean falling = true;
protected boolean jumping = false;
public GameObject(float x, float y, ObjectId id)
{
this.x = x;
this.y = y;
this.id = id;
}
public abstract void tick(LinkedList<GameObject> object);
public abstract void render(Graphics g);
public abstract Rectangle getBounds();
public float getX()
{
return x;
}
public float getY()
{
return y;
}
public void setX(float x)
{
this.x = x;
}
public void setY(float y)
{
this.y = y;
}
public float getVelX()
{
return velX;
}
public float getVelY()
{
return velY;
}
public void setVelX(float velX)
{
this.velX = x;
}
public void setVelY(float velY)
{
this.velY = y;
}
public boolean isFalling() {
return falling;
}
public void setFalling(boolean falling) {
this.falling = falling;
}
public boolean isJumping() {
return jumping;
}
public void setJumping(boolean jumping) {
this.jumping = jumping;
}
public ObjectId getId()
{
return id;
}
}
Game.java:
package com.cosmicluck.miner.window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
import com.cosmicluc.miner.framework.KeyInput;
import com.cosmicluc.miner.framework.ObjectId;
import com.cosmicluc.miner.objects.Player;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = -905290418752159869L;
private boolean running = false;
private Thread thread;
public static int WIDTH, HEIGHT;
//Object
Handler handler;
Random rand = new Random();
private void init()
{
WIDTH = getWidth();
HEIGHT = getHeight();
handler = new Handler();
handler.addObject(new Player(100, 100, handler, ObjectId.Player));
handler.createLevel();
this.addKeyListener(new KeyInput(handler));
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void run()
{
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick()
{
handler.tick();
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
////////////////////////////////////
//Draw here
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
handler.render(g);
////////////////////////////////////
g.dispose();
bs.show();
}
public static void main(String args[])
{
new Window(800, 600, "Miner Madness - Prototype", new Game());
}
}
提前谢谢你!
编辑:固定! 当我分别制作velX / velY = x,y时,我的GameObject中出现了一个愚蠢的错误。谢谢大家的帮助! :)
答案 0 :(得分:0)
public void setVelX(float velX)
{
this.velX = x;
}
public void setVelY(float velY)
{
this.velY = y;
}
GameObject中的setter不正确。
答案 1 :(得分:0)
您应该将方法参数分配给字段,而不是其他字段。
public void setVelX(float velX)
{
this.velX = velX;
}
public void setVelY(float velY)
{
this.velY = velY;
}