简体   繁体   English

如何在AnimationTimer循环JavaFX中创建对象生成器

[英]How to make Object generator in the AnimationTimer loop JavaFX

I'm making simple spaceship game on JavaFX. 我正在JavaFX上制作简单的太空飞船游戏。 I have class SpaceShip and i want to make random objects from this class in my main game loop on random starting position(in interval of 5 seconds for example). 我有SpaceShip类,并且我想在我的主游戏循环中从随机开始位置(例如,以5秒为间隔)中的此类中创建随机对象。 I try to use Timer schedule() method for the task. 我尝试将Timer schedule()方法用于该任务。 The problem is that i can't get clear image of the spaceship, it disappears and show in other point because of the constantly looping. 问题是我无法获得清晰的飞船图像,由于不断循环,它消失并显示在其他位置。 Can someone help me with advice how to handle with this. 有人可以为我提供如何处理的建议。

My game loop: 我的游戏循环:

 new AnimationTimer() { @Override public void handle(long currentNanoTime) { double t = (currentNanoTime - startNanoTime) / 1000000000.0; double xMoving = ((100 * t) % (canvas.getWidth() + 100)); double x = 232 + 128 * Math.cos(t); double y = 232 + 128 * Math.sin(t); //background image clears canvas gc.drawImage(space, 0, 0); gc.drawImage(earth, x, y); gc.drawImage(sun, 196, 196); // draw UFO gc.drawImage(ufo.getFrame(t), 100, 25); //draw spaceShip SpaceShip.generate(new SpaceShip(spaceShipImageArr, 0.100, gc, t, xMoving - 100, (randomNum + 150))); //timer schedule Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { SpaceShip.generate(new SpaceShip(spaceShipImageArr, 0.100, gc, t, xMoving - 100, (randomNum + 230))); } }, 5000); } }.start(); 

And the SpaceShip class: 还有SpaceShip类:

package objectClasses;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;


public class SpaceShip{

private final GraphicsContext gc;
private final double frame;
private final double y;
private final double x;
private AnimatedImage object;

public SpaceShip(Image[] arr, double duration, GraphicsContext gc, double frame, double x, double y) {
    object = new AnimatedImage();
    this.object.frames = arr;
    this.object.duration = duration;
    this.gc = gc;
    this.frame = frame;
    this.y = y;
    this.x = x;
}

private void drawShip() {
    this.gc.drawImage(this.object.getFrame(frame), x, y);
}

public static void generate(SpaceShip spaceShip) {
    spaceShip.drawShip();
}

} }

This is the culprit. 这是元凶。

SpaceShip.generate(new SpaceShip(spaceShipImageArr, 0.100, gc, t, xMoving - 100, (randomNum + 150)));

Basically, in each frame you create a new instance of SpaceShip . 基本上,在每个框架中,您都会创建一个新的SpaceShip实例。 The blinking images are caused by space ships being created over and over again. 图像闪烁是由一遍又一遍的太空飞船造成的。 That won't work. 那行不通。

You need to create these objects outside the game loop and store their references: 您需要在游戏循环之外创建这些对象并存储其引用:

SpaceShip mySpaceShip = new SpaceShip(spaceShipImageArr, 0.100, gc, t, xMoving - 100, (randomNum + 150));

In the game loop you just update the position of your actors (that part is missing) and keep drawing them at new positions. 在游戏循环中,您只需更新演员的位置(缺少该部分),然后将其绘制在新位置即可。

SpaceShip.generate(mySpaceShip);

Side note: you can remove the generate method altogether, make drawShip public, rename it to draw and simply call mySpaceShip.draw() - it will be more evident what's the role of your methods. 旁注:您可以完全删除generate方法,将drawShip公开,将其重命名为draw并简单地调用mySpaceShip.draw() -这将更加明显地说明方法的作用。 It's perfectly acceptable for an object to draw itself. 一个对象自己绘制是完全可以接受的。

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

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