1

I am working on a game using javafx. I have coded some games in awt before, so I have a basic sense of how games work. (I used bufferedimages) However, with so all the new stuff in the javafx api, I am questioning if my old game architecture still applies to javafx. This is the current logic of my game:

enter image description here

This is how my rendering works:

List<Entity> entities= new ArrayList<Entity>;
GraphicsContext gc;  ///imagine that I created a new GraphicsContext object

inside the render method it basically looks like this:

  for(Entity e : entities)
 {
  gc.drawImage(e.getX(),e.getY(),e.getImage());
 }

However, since javafx has so many new features, I am wondering if this approach is obsolete or inefficient. Should I move these entities or should I move the Image themselves? I am working on a space shooting game, so will Interpolate() and Transition improve my game?

There are not a lot of specific javafx game tutorials available, so I have to seek some help here... Thank you!

Ptolemorphism
  • 101
  • 2
  • 11
  • Look at `Timeline`, for [example](https://stackoverflow.com/q/39185306/230513). – trashgod Oct 15 '17 at 16:27
  • @trashgod I am currently using an AnimationTimer for my loop. What do you think about making my Entity class a subclass of ImageView? – Ptolemorphism Oct 15 '17 at 16:29
  • That might be a bad Idea because drawing an Image doesn't need that much memory like an ImageView does. ImageView gives you way more unnecessary Options and shouldn't be used for Game development! – Aaron Stein Oct 15 '17 at 16:41
  • @AaronStein Thanks for replying! I did try to use image, but when my entity moved, the old images stayed on the background instead of going away... Should I redraw the background every time? – Ptolemorphism Oct 15 '17 at 16:43
  • You need to overwrite the backgound – Aaron Stein Oct 15 '17 at 16:44
  • @AaronStein Ok. Do you know any tutorial that explains the main architecture of javafx games? (Loop, Entity, animation, etc) – Ptolemorphism Oct 15 '17 at 16:46
  • @Ptolemorphism I used back than this: https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835 but it's only the beginning – Aaron Stein Oct 15 '17 at 16:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156780/discussion-between-ptolemorphism-and-aaron-stein). – Ptolemorphism Oct 15 '17 at 16:48

1 Answers1

2

The way you are doing is in my opinion the best way to do it, I have done something similar and used an entity like this: package jumpNRun.gameObjects;

import java.util.List;
import javafx.scene.image.Image;
import javax.xml.bind.annotation.XmlRootElement;
import jumpNRun.gameObjects.entitys.Solid;


@XmlRootElement
public class Entity {

    protected Image entityImage;

    protected double gravity = 0;

    protected int height = 0, width = 0;

    protected int x = 0, y = 0;

    protected int id = 0;
    /**
     * Class speciefed informations
     */
    protected String extraInfo;
    protected boolean killed = false;

    public String getExtraInfo() {
        return extraInfo;
    }

    public void setExtraInfo(String extraInfo) {
        this.extraInfo = extraInfo;
    }

... and so on The drawing can be done like this:

    gc.drawImage(backround, 0, 0);
    for (Entity e : entitys) {
        gc.drawImage(e.getEntityImage(), e.getX(), e.getY());
    }
Aaron Stein
  • 526
  • 7
  • 18