我有一个球员和子弹。子弹在固定的地方产卵。如何让它们在我的播放器前面产生?如何将我的播放器X和Y放入下面的代码中?我想不明白。 这是KeyInput类。空间拍摄。当我开枪时,我希望子弹能够在我的玩家身上产生:
package Game;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import Game.Game.STATE;
public class KeyInput extends KeyAdapter{
private Handler handler;
private boolean[] keyDown = new boolean[4];
Game game;
public KeyInput(Handler handler, Game game){
this.handler = handler;
this.game = game;
keyDown[0] = false;
keyDown[1] = false;
keyDown[2] = false;
keyDown[3] = false;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
// Player Movement
if(tempObject.getid() == ID.Player){
if(key == KeyEvent.VK_W) { tempObject.setVelY(-5); keyDown[0] = true; }
if(key == KeyEvent.VK_S) { tempObject.setVelY(5); keyDown[1] = true; }
if(key == KeyEvent.VK_A) { tempObject.setVelX(-5); keyDown[2] = true; }
if(key == KeyEvent.VK_D) { tempObject.setVelX(5); keyDown[3] = true; }
//Player Shoot
if(key == KeyEvent.VK_SPACE){
handler.addObject(new PlayerBullet(400, 400, ID.PlayerBullet, handler));
}
}
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
// Player Movement Release
if(tempObject.getid() == ID.Player){
if(key == KeyEvent.VK_W) keyDown[0]=false; //tempObject.setVelY(0);
if(key == KeyEvent.VK_S) keyDown[1]=false; //tempObject.setVelY(0);
if(key == KeyEvent.VK_A) keyDown[2]=false; //tempObject.setVelX(0);
if(key == KeyEvent.VK_D) keyDown[3]=false; //tempObject.setVelX(0);
// Y Movement
if(!keyDown[0] && !keyDown[1]) tempObject.setVelY(0);
// X Movement
if(!keyDown[2] && !keyDown[3]) tempObject.setVelX(0);
}
}
if(key == KeyEvent.VK_P){
if(Game.gameState == STATE.Game){
if(Game.paused) Game.paused = false;
else Game.paused = true;
}
}
if(key == KeyEvent.VK_ESCAPE){
System.exit(1);
}
}
}
这是Player类:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;
public class Player extends GameObject {
public static float PlayerHealth = 100;
Random r = new Random();
Handler handler;
public Player(int x, int y, ID id, Handler handler){
super(x, y, id);
this.handler = handler;
}
public Rectangle getBounds(){
return new Rectangle((int)x, (int)y, 32, 32);
}
public void tick(){
timer--;
x += velX;
y += velY;
PlayerHealth = (int)Game.clamp(PlayerHealth, 0, 100);
x = Game.clamp((int)x, 0, Game.WIDTH - 38);
y = Game.clamp((int)y, 0, Game.HEIGHT - 61);
handler.addObject(new Trail((int)x, (int)y, ID.Trail, Color.GREEN, 32, 32, 0.1f, handler));
handler.addObject(new PlayerBullet((int)x+16, (int)y-12, ID.PlayerBullet, handler));
collision();
}
private void collision(){
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if (tempObject.getid() == ID.BasicEnemy || tempObject.getid() == ID.PlayerBullet){
if(getBounds().intersects(tempObject.getBounds())){
// Collision code
HUD.PlayerHealth -= 2;
}
}
}
}
public void render(Graphics g){
// Show borders
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.GREEN);
g2d.draw(getBounds());
g.setColor(Color.GREEN);
g.fillRect((int)x, (int)y, 32, 32);
}
}
我的播放器移动键位于KeyInput中,所以我认为它可以访问。我只需要弄清楚如何将当前的400,400更改为我的玩家X和Y.
答案 0 :(得分:1)
我解决了。
handler.addObject(new PlayerBullet((int)tempObject.getX()+10, (int)tempObject.getY()-12, ID.PlayerBullet, handler));
我忘记了tempObject.getY中的()
我很蠢: 解决...