繁体   English   中英

Java中的计时器加速

[英]Timer speeding up in Java

我知道还有其他问题,但是似乎没有一个问题可以帮助我解决问题。 在我的游戏中,有一个射击导弹的玩家,向负y飞行,直到其消失在屏幕外,然后回到其原始位置并等待再次射击。 但是,每次发射导弹时,它都会变得更快,就像Timer()中的延迟变得更短一样。 编码:

主班:

public class ShooterGame extends JFrame{
    static int playerX=500;
    static int playerY=520;

    InputHandler input = new InputHandler(this);
    public static Player player = new Player(playerX,playerY,50,50);

    public static void main(String[] args){
        ShooterGame game = new ShooterGame();
        game.run();
        System.exit(0);
    }

    static int windowWidth = 1300;
    static int windowHeight = 600;
    static int fps = 30;
    public static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
    public static Graphics bbg;

    public void run(){
        boolean running = true;

        initialize();

        while(running){
            long time = System.currentTimeMillis();

            update();
            draw();

            time = (1000 / fps) - (System.currentTimeMillis() - time);

            if (time > 0) { 
                try{ 
                    Thread.sleep(time); 
                } 
                    catch(Exception e){}; 
            };
        }

    }

    public void initialize(){
        setTitle("--- Shooter Game ---");
        setSize(windowWidth, windowHeight);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void update(){
        player.update(input);
    }

    public void draw(){

        Graphics g = getGraphics(); 

        Graphics bbg = backBuffer.getGraphics(); 

        bbg.setColor(Color.BLACK); 
        bbg.fillRect(0, 0, windowWidth, windowHeight); 
        player.Draw(bbg);
        enemie1.Draw(bbg,10,100);

        if(game.player.Player.missileRunning) game.player.Player.missile.Draw(bbg);

        g.drawImage(backBuffer, 0, 0, this); 
    }

    public static Graphics getMainGraphics(){
        return bbg;
    }
}

玩家等级:

public class Player{

    private BufferedImage sprite;
    public BufferedImage missileSprite;
    public int x, y, width, height;
    private int fixedX;
    private final double speed = 5.0d;
    public static Missile missile;
    public static boolean missileRunning = false;
    public static boolean missileReady = true;

    public Player(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        missile = new Missile(this.x);

        try{
            URL url = this.getClass().getResource("ship.png");
            sprite = ImageIO.read(url);
        } catch(IOException e){}

        try{
            URL url2 = this.getClass().getResource("missile.png");
            missileSprite = ImageIO.read(url2);
        } catch(IOException e){}
    }

    public void keyPlayer(double delta, InputHandler i){
        if(i.isKeyDown(KeyEvent.VK_D)){
            if(this.x>=1240) return;
            else this.x+=speed*delta;
        }

        if(i.isKeyDown(KeyEvent.VK_A)){
            if(this.x<=0) return;
            else this.x-=speed*delta;
        }

        if(i.isKeyDown(KeyEvent.VK_SPACE)){
            if(missileReady){ 
                try {
                    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:/Users/Gabriel/Desktop/Programacao/Other/java/ShootGame/game/player/Fire.wav").getAbsoluteFile());
                    Clip clip = AudioSystem.getClip();
                    clip.open(audioInputStream);
                    clip.start();
                } catch(Exception ex) {
                    System.out.println("Error with playing sound.");
                    ex.printStackTrace();
                }
                missile.x=this.x + 20;
                missileRunning=true; 
            }
        }

    }

    public void update(InputHandler inputP){
        keyPlayer(2.0, inputP);
        updateMissile(game.ShooterGame.backBuffer.getGraphics());
    }

    public void updateMissile(Graphics g){
        if(missileRunning){ 
            missileReady=false;
            missile.update(g);
        }
    }

    public Rectangle missileBounds(){
        return new Rectangle(missile.x, game.player.Missile.y, 6, 18);
    }

    public void Draw(Graphics a){
        a.drawImage(sprite,x,y,width,height,null);
    }

    public Rectangle getBounds(){
        return new Rectangle(x,y,width,height);
    }
}

导弹舱:

public class Missile{

    public BufferedImage sprite;
    public static int x;
    public static int y=504;
    private int interval = 2000;
    private Timer timer2;
    private boolean isAlive = game.player.Player.missileRunning;
    static final AtomicInteger count = new AtomicInteger();
    public static boolean timerReady;

    public Missile(int x){

        this.x=x;
        this.y=504;
        this.sprite=sprite;

        try{
            URL url = this.getClass().getResource("missile.png");
            sprite = ImageIO.read(url);
        } catch(IOException e){System.out.println("Error loading image");}  
    }

    public void Draw(Graphics g){
        g.drawImage(sprite,x,this.y,6,18,null);
    }

    public void update(Graphics g){ //The problem
        if(game.player.Player.missileRunning==true){
            timerReady=true;
            if(checkTimer()){
                timer2 = new Timer();
                timer2.schedule(new Move(), 0, interval);
            }
            this.y = y;
            if(y <= 0){
                game.player.Player.missileRunning=false;
                timerReady=false;
                y=504;
                if(!checkTimer()){
                    timer2.cancel(); 
                    timer2.purge();
                }
                timer2=null;
                reload();
            }
        }
    }

    public boolean checkTimer(){
        if(timerReady){
            return true;
        } else {
            return false;
        }
    }

    class Move extends TimerTask{
        public void run(){
            int keeper = 3;
            if(keeper>0) y-=interval/1000;
        }
    }

    public synchronized void reload(){
        Timer missileBetween = new Timer();

        missileBetween.cancel();
        missileBetween = new Timer();

        TimerTask readyMissile = new TimerTask(){
            public void run(){
                game.player.Player.missileReady=true;
            }
        };

        missileBetween.schedule(readyMissile, 20);
    }

    public static int getNumber(){
        return count.get();
    }

    public static AtomicInteger getAtomic(){
        return count;
    }

}

(我并没有发布我的所有程序,只是发布了与问题相关的部分) (如果缺少任何内容,请说出它)

谢谢

没有运行代码...

timerReady=true;
if(checkTimer()){
    timer2 = new Timer();
    timer2.schedule(new Move(), 0, interval);
}

基本上意味着checkTimer总是会返回true ,这意味着每次调用update ,您都在创建一个ANOTHER Timer ,这可能会创建几十个计时器,所有这些计时器都是独立更新的……这将导致对象加速...

这种逻辑和功能应由主循环控制。 在每个周期中,您应该检查对象的状态并做出移动或删除对象的决定,此处不应涉及其他“计时器”或“循环”。

哦,要学会在没有static情况下做,这会给您带来更多的问题,然后在这种情况下会解决。

和...

Timer missileBetween = new Timer();
missileBetween.cancel();
missileBetween = new Timer();

...毫无意义,您可以创建一个本地Timer ,然后取消它,即使它实际上并没有运行,创建一个新的本地实例并安排一些任务...

您丢失了对本地版本的引用,无法再对其进行修改...

您在ShooterGame.run()方法中睡眠的时间是错误的。 而不是time = (1000 / fps) - (System.currentTimeMillis() - time); 为什么不简单地将time = 1000/fps

首次进入运行time循环的while time约为1000/fps因为两次方法调用之间的时间很少。 未来的运行周期将为您提供一个负数(System.currentTimeMillis()与1000 / fps相比是一个很大的数字),这意味着您将不会调用Thread.sleep(time) 在此处设置一个断点,然后告诉我们这是否正确。

暂无
暂无

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

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