[英]My bullet is not being fired when i press the space bar
i'm teaching myself to program in java, and i have decided to make an space invader game. 我正在教自己用Java编程,并且我决定制作一款太空入侵者游戏。 i got my ship moving in any direction, but i have a problem with my bullet being fired.
我的船向任何方向移动,但是我的子弹被发射了。 now i know that my y-coordinate of the bullet are being updated every time my ship moves, but it is not firing.
现在我知道我的飞船的y坐标每次我的船移动时都会更新,但是它没有发射。 I need someone to help me get it to fire if possible.
如果可能的话,我需要有人帮助我。 any help is welcome.
欢迎任何帮助。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements KeyListener, ActionListener{
private int x;
private int y;
private int dx;
private int dy;
int bx;
int by;
Rectangle bullet;
Timer timer;
private Image image;
public Panel() {
timer = new Timer(30, this);
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);
timer.start();
x=130;
y=430;
bx=xPost()+55;
by=yPost();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon ii= new ImageIcon("C:\\Users\\TriZam\\workspace\\LearningSprite\\ship.png");
image=ii.getImage();
g.drawImage(image, x, y, this);
doDrawing(g);
}
public void move(){
// thhis method will be placed inside the interferance ActionPerformed in order to move the ship and bullet
x += dx;
y += dy;
bx += dx;
by += dy;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -3;
if (x<=-25){
dx=0;
}
}
if (key == KeyEvent.VK_RIGHT) {
dx = 3;
if (x>=380 ){
dx=0;
}
}
if (key == KeyEvent.VK_UP) {
dy = -3;
if (y<=0 ){
dy=0;
}
}
if (key == KeyEvent.VK_DOWN) {
dy = 3;
if (y>=430 ){
dy=0;
}
}
if(key ==KeyEvent.VK_SPACE){
// bullet shooting and as you can see the y coordinate updates but bullet not moving.
bullet.y--;
System.out.println(bullet.y--);
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
move();
repaint();
}
int yPost(){
return y;
}
int xPost(){
return x;
}
void doDrawing(Graphics g) {
bullet = new Rectangle(bx, by, 10, 10);
g.setColor(Color.white);
g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
}
This is the main class 这是主班
import javax.swing.JFrame;
public class MainClass extends JFrame {
private int FrameWidth;
private int FrameHeigh;
private Panel panel;
public MainClass(int width, int height ) {
panel= new Panel();
this.FrameWidth=width;
this.FrameHeigh=height;
setSize(FrameWidth,FrameHeigh);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
MainClass m= new MainClass(500, 600);
}
}
create the bullet once ; 一次创造子弹; not inside paint and drawing - inside the constructor;
不在绘画中-在构造函数中;
move this out of 将其移出
void doDrawing(Graphics g) {
--> remove this from here and into the constructor --> bullet = new Rectangle(bx, by, 10, 10);
also when you update the location of the bullet dont use bx, by and separate variables; 另外,当您更新项目符号的位置时,请勿使用bx,by和单独的变量; just do
做就是了
bullet.x=....new location
bullet.y=....new location
Inside your key pressed space event, you should create an instance of a bullet. 在按键按下的空间事件中,您应该创建一个项目符号实例。 As of right now, you have a member variable Bullet;
截至目前,您有一个成员变量Bullet; however it is null since it's never been initialized.
但是它为null,因为它从未被初始化。 That being said, you'll also want to move your bullet--this should be done in your move function.
话虽如此,您还需要移动子弹-这应该在move函数中完成。
I see that you have a bx, by, which I'm assuming is suppose to be the bullet's x and y position. 我看到您有一个bx,据此假设是子弹的x和y位置。 However, the rectangle's coordinates never update.
但是,矩形的坐标永远不会更新。 Instead of incrementing bx and by, update the rectangle (You'll also want to repaint the rectangle).
更新矩形(而不是重新绘制矩形),而不是增加bx和by。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.