繁体   English   中英

子弹射击不正确

[英]Bullet Not shooting correctly

单击时,我一直试图进行子弹射击。 它部分起作用,但问题是它随船一起移动,而不是直接向上移动(就像我希望它前进)。

我认为这可能是由于xy坐标都指向飞船这一事实的缘故。 这是我的代码:

 package galaga.display.players;

import galaga.display.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet {
    private  int x;
    private  int y;
    private final int size = 16;
    public boolean hasClicked;
    Rectangle bullet;
    public Bullet() {


        setX(Game.getPlayer1().getX()+Game.getPlayer1().getSize()/3);
        setY(Game.getPlayer1().getY()-Game.getPlayer1().getSize());

    }
    private void setX(int x){
        this.x = x;
    }
    private void setY(int y){
        this.y = y;
    }
    public int getY(){
        return y;
    }
    public int getX(){
        return x;
    }
    public void render(Graphics g){
        if(hasClicked){
            g.setColor(Color.red);
            g.fillOval(x,y, size, size);
        }

    }

    public void update(Game game){

        if(hasClicked){


            y--;

        }

    }
}

这是我的轮船/出租人舱:

package galaga.display.players;

import galaga.display.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Players {
    public boolean isUp,isDown,isLeft,isRight;
    private int x, y;
    private int size = 50;
    private BufferedImage image2;
    private Rectangle boundingBox;
    boolean hasCollided = false;
    public Players(int x,int y) {
        setY(y);
        setX(x);
        try {
            image2 = ImageIO.read(new File("C:\\Users\\Anonymous\\Desktop\\SpaceShip.png"));
        } catch (IOException e) {

            e.printStackTrace();
        }
        setBoundingBox(new Rectangle(x,y,size,size));

    }
    private void setY(int y){
        this.y = y;
    }
    private void setX(int x){
        this.x = x;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
    public int getSize(){
        return size;
    }
    public void render(Graphics g){


        g.setColor(Color.black);
        g.drawImage(image2,x,y,size,size,null);

    }
    public void update(Game game){
        Rectangle rect = game.getRectangle();
        getBoundingBox().setBounds(x, y, size, size);

        if(isUp){
            y--;
        }
        else if (isDown){
            y++;
        }
        else if(isLeft){
            x--;
        }
        else if(isRight){
            x++;
        }

        if (x < 0 || x + size >= rect.width) {
            // change the direction
            x *= -1;
        }
        if (y < 0 || y + size >=rect.height) {
            // change the direction
            y*= -1;
        }



    }
    public Rectangle getBoundingBox() {
        return boundingBox;
    }
    public void setBoundingBox(Rectangle boundingBox) {
        this.boundingBox = boundingBox;
    }

}

静态变量:正如许多人所说的那样,代码存在一些问题,超出了项目符号所在的位置。 首先,您的静态变量不应为静态。

静态变量不是对象的每个实例唯一的,并且在该类的所有对象中保持不变。 (即每个子弹将具有相同的x y坐标)。

更新方法:我也不知道您何时或如何调用update()方法,但是如果您仅在用户单击时调用update方法,则子弹只会在用户单击时移动... IE他们必须通过单击将项目符号弹出屏幕。

您可能希望在主游戏的update方法中调用update方法,以便使用游戏本身来更新和重画项目符号。

最后,您的更新方法不应该创建新的项目符号,它只需要更新该项目符号的xy坐标并重绘它,或者在您的情况下,只需绘制y变量即可。

package galaga.display.players;

import galaga.display.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet {
    private int x;
    private int y;
    private final int size = 16;
    Rectangle bullet;
    public Bullet() {

        setX(Game.getPlayer1().getX()+Game.getPlayer1().getSize()/3);
        setY(Game.getPlayer1().getY()-Game.getPlayer1().getSize());
    }
    private void setX(int x){
        this.x = x;
    }
    private void setY(int y){
        this.y = y;
    }
    public int getY(){
        return y;
    }
    public int getX(){
        return x;
    }
    public static void render(Graphics g){  // Call this in your main game's paint method

            g.setColor(Color.red);
            g.fillOval(x,y, size, size);
        }
    }

    public static void update(Game game){
     //removed the creating a new bullet from the update.
            y--;
    }
}

最后,我删除了hasClicked变量。 项目符号带有hasClicked变量没有多大意义,因为项目符号永远不会在乎用户是否单击,这是主要游戏应该担心的事情。

另外,您在构造函数中将hasClicked设置为true,并且从不修改该值或将其更改为false,因此它始终为true,因此毫无意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM