如何防止矩形对象在被调用时消失

时间:2018-12-06 02:53:22

标签: java

因此,在我制作太空射击游戏的Java游戏中,我正在制作从特定给定(x,y)位置射击并上升的激光雷达,我编写了以下代码

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;
import java.awt.AlphaComposite;
public class Lazers extends Rectangle{
    int speedy = -5;
    float alpha = 0.6f;
    AlphaComposite hue = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    public Lazers(int x, int y) {
        this.setSize(7,35);
        this.setLocation(x,y);
    }
    /*
    public Lazers (int x, int y) {
        this.setSize(5,15);
        this.setLocation(x, y);
    }
    */

    public void shooter(Graphics2D win) {
        win.setColor(Color.BLUE);
        win.setComposite(hue);
        win.fill(this);

        this.translate(0, speedy);

    }
}

在另一个类中,我执行了以下操作(剪裁了代码以使其更小),并注意GameMode1,因为这是我编写代码的位置):

公共类finalshooter扩展了GameDriverV4实现的ActionListener,MouseListener {

int level=1;                                            
int gamestate = 0;                                      
int counter = 0;
int speedo = 5;
int spacex = 25;
int spacex_ = 25;
int spacey = 480;
int spacey_=480;
int particlecount = 200;
boolean test = false;
blackbear apr = new blackbear();                        
BufferedImage shooter;
//Lazers bullet = new Lazers(spacex+454,spacey+220);
Lazers bullet = null;
//bullet = new Lazers(spacex+454,spacey+220);s
//Lazers bullet = new Lazers(spacex+454,spacey+220);


public finalshooter() {                                 //adds random digit from 1-4 to the arrays to store 
    shooter = this.addImage("bigger.png");




    for (int apr = 0; apr<=particlecount-1; apr++) {
        Random shit = new Random();
        int randoy = shit.nextInt(800);
        test1[apr]=new topdownparticles(randoy);
    }


}
public static void main(String[] args) {
    finalshooter john = new finalshooter();
    john.start();
    System.out.println("this is running");

}

public void Splash(Graphics2D win) {

}
/*
public void lyn(MouseListener e) {

}
*/
public void kenalyn(MouseEvent e) {

}


@Override
                                                    //this is where all of the coding happens


public void draw(Graphics2D win) {
    // TODO Auto-generated method stub
    if (gamestate ==2) {
        //help page
        this.Splash(win);

        Font font2 = new Font("Trattatello", Font.BOLD, 80);
        win.setFont(font2);
        win.setColor(Color.YELLOW);
        win.drawString("Instruction:", 250, 150);
        Font font3 = new Font("Luminari", Font.BOLD,20);
        win.setFont(font3);

        if (GameDriverV4.Keys[KeyEvent.VK_M]) {                         //goes to menu
            gamestate = 0;
        }

    }

    if (gamestate == 3) {
        [this part of code taken out for shortness]
    }
    if (gamestate == 1) {
        this.Splash(win);
        win.setColor(Color.white);
        win.drawImage(shooter, null, spacex,spacey);


        [this part of code taken out for shortness]

        bullet = new Lazers(spacex+454,spacey+220);
        bullet.shooter(win);
        //bullet.shooter(win);


        /*
         * 
         * spacex+454,spacey+220
         * 
         */

        /*
        win.fillRect(kenichi.x, kenichi.y, kenichi.width, kenichi.height);
        kenichi.translate(speedo, 0);
        if(kenichi.getX()<=0) {
            speedo = Math.abs(speedo);  
        }
        if (kenichi.getX()>=800) {
            speedo = -1*Math.abs(speedo);
        }
        */
        //particle = new topdownparticles[20];
        // version two of the particles
        for (int k= 0; k<=particlecount-1; k++) {
            test1[k].moveparticlefast(win,k);

        }
        for (int e = 0; e<=particlecount-1; e++) {
            if (test1[e].getY()>=800) {
                Random variable = new Random();
                int newyloc = variable.nextInt(800);
                test1[e]= new topdownparticles(newyloc);
                test1[e].moveparticlefast(win, e);
            }

        }


    }

    if (gamestate == 0) {
        this.Splash(win);
                    [this part of code taken out for shortness]
        if (GameDriverV4.Keys[KeyEvent.VK_O]) {                     //goes to option oage
            gamestate = 3;
        }
        if (GameDriverV4.Keys[KeyEvent.VK_H]) {                     //goes to help page
            gamestate = 2;
        }
        if (GameDriverV4.Keys[KeyEvent.VK_ENTER]) {                 //goes to the actual game
            gamestate = 1;
        }
        win.drawImage(shooter, null, spacex_ ,spacey_);


    }




}
[all override stuff taken out for shortness]
}

}

那是什么问题?

正在发生的事情是无限次调用Lazer的Shooter方法,并且看起来像是在宇宙飞船所在的位置,矩形始终一直粘贴在那里,如下面的图片所示

enter image description here

0 个答案:

没有答案