简体   繁体   English

子弹射击不正确

[英]Bullet Not shooting correctly

I've been trying to make a bullet shoot when I click. 单击时,我一直试图进行子弹射击。 It partly works, but the thing is that it's moving with the ship instead up directly up (like I want it to go). 它部分起作用,但问题是它随船一起移动,而不是直接向上移动(就像我希望它前进)。

I think this may be due to the fact that the x and y coordinates are pointing to the ship is there any way around this? 我认为这可能是由于xy坐标都指向飞船这一事实的缘故。 Here is my code: 这是我的代码:

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

        }

    }
}

Here is my 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;
    }

}

Static Variables: As many have stated there are a few issues with the code beyond the bullet not being where you want it to be. 静态变量:正如许多人所说的那样,代码存在一些问题,超出了项目符号所在的位置。 First of all your static variables should not be static. 首先,您的静态变量不应为静态。

Static variables are not unique to each instance of the object and remain constant across all objects of that class. 静态变量不是对象的每个实例唯一的,并且在该类的所有对象中保持不变。 (IE Every bullet will have the same x y coordinates). (即每个子弹将具有相同的x y坐标)。

Update Method: Also I have no idea when or how you are calling the update() method, but if you are only calling the update method when a user clicks then your bullet is only going to move when a user clicks... IE they have to pump the bullet up the screen via clicking. 更新方法:我也不知道您何时或如何调用update()方法,但是如果您仅在用户单击时调用update方法,则子弹只会在用户单击时移动... IE他们必须通过单击将项目符号弹出屏幕。

You probably want to call the update method in your main game's update method so that the bullet is updated and redrawn with the game itself. 您可能希望在主游戏的update方法中调用update方法,以便使用游戏本身来更新和重画项目符号。

Lastly your update method should not be creating new bullets, it just needs to update the x and y coordinates of that bullet and redraw it, or in your case, just the y variable. 最后,您的更新方法不应该创建新的项目符号,它只需要更新该项目符号的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--;
    }
}

Lastly I removed the hasClicked variable. 最后,我删除了hasClicked变量。 It doesn't make much sense to have a bullet with a hasClicked variable, since the bullet should never care if a user is clicking, that's something the main game should worry about. 项目符号带有hasClicked变量没有多大意义,因为项目符号永远不会在乎用户是否单击,这是主要游戏应该担心的事情。

Additionally you set hasClicked to true in the constructor, and never modified the value or changed it to false so it will always be true and therefore it's pointless anyhow. 另外,您在构造函数中将hasClicked设置为true,并且从不修改该值或将其更改为false,因此它始终为true,因此毫无意义。

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

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