Bullet不能正确拍摄

时间:2014-03-11 01:12:34

标签: java animation graphics coordinates

当我点击时,我一直试图射击子弹。它部分起作用,但问题是它正在与船一起向上移动(就像我希望它一样)。

我认为这可能是由于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--;

        }

    }
}

这是我的Ship / PLayer Class:

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;
    }

}

1 个答案:

答案 0 :(得分:0)

静态变量 正如许多人所说,除了子弹之外的代码存在一些问题,而不是你想要它的地方。首先,你的静态变量不应该是静态的。

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

更新方法: 此外,我不知道你何时或如何调用update()方法,但如果你只是在用户点击时调用更新方法,那么当用户点击时,你的子弹只会移动... IE他们有通过点击将子弹泵入屏幕。

您可能希望在主游戏的更新方法中调用更新方法,以便更新子弹并重新绘制游戏本身。

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

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设置为true,并且从未修改过该值或将其更改为false,因此它始终为true,因此无论如何都无意义。

相关问题