t

This is the second installment in a series of blog entries related to a javafx 2 gaming tutorial. If you haven’t read Part 1, please check out the

introduction

javafx 2 game tutorial section. To recap in part 1, I mention some aspects of the game and a simple demo of a prototype spaceship (made up of simple shapes) that can navigate with the mouse.

disclaimer

: it’s a long tutorial so if you just want to run the demo just

Click on

here

. the demo is called atom smasher where you generate atoms (spheres) which collide. you can freeze the game to add more atoms. the goal is to have more than one living and bouncing atom. a text displays the current number of atoms floating around. Before we start our discussion of a game loop, I wanted to give you a history of games and animation.

Back then (during the 80s-90s) many game programmers attempting to animate images encountered the infamous screen flickering problem. this is where your

sprites

(graphic images) will often flicker and make the game pretty awful. all monitors have a refresh rate where certain intervals the pixels will be redrawn (called

vertical tracing crts

). for example, if the refresh rate is 80Hz, the screen is redrawn about 80 times per second. If you change things on the screen, you can often get out of sync due to a refresh interval. what should you do about it? well, actually there are two things that will help with this problem (double buffering and knowing when the cycle is happening). some smart developers have created a technique called double buffering.

double buffer

is a technique that consists of two surfaces where each in turn becomes the displayable surface and the other is an off-screen area (buffer surface). this technique is really a digital sleight of hand where the developer can pre-calculate the sprites and their positions to draw on the off-screen surface. once you are done drawing on the offscreen pad, the code will change it as a displayable surface. an important thing to point out is that we still have a problem due to the fact that we need to be notified when the refresh interval is about to start the refresh process. in java, this capability is integrated via the

buffer strategy

api. so where am i going with this? sometimes explaining past strategies will help us appreciate what we have today. do we have to do it in javafx? Nope. fear not javafx is here! all the issues i mentioned are all fixed for us using javafx’s scene chart API. However, most of the games still use the old method of animating graphics and updating the game world known as the game loop.

Simply put, the game loop is responsible for updating sprites (graphics), checking for collisions, and cleaning up. older game loops will check for key and mouse events as part of the loop. since javafx abstracts events to allow scene or individual nodes to handle events, the ability to listen to low level events is not needed in our game loop. below is a source code snippet from ‘a typical game loop that will update sprites, check for collisions, and clean up sprites every cycle. you will notice the javafx 2.x duration object which is 60 divided by 1000 milliseconds or 60 frames per second (fps). each frame will call the

manipulate()

javafx method

Event Manager

interface in order to update the game world. hypothetically, I created three methods

updatesprites ()

,

check for collisions ()

, and

cleanupsprites ()

which will be invoked to manage the sprites in the game.

final duration oneframeamt = duration.millis(1000/60);
final keyframe oneframe = new keyframe(oneframeamt,
   new eventhandler() {

   @override
   public void handle(javafx.event.actionevent event) {

      // update actors
      updatesprites();

      // check for collision
      checkcollisions();

      // removed dead things
      cleanupsprites();

   }
}); // oneframe

// sets the game world's game loop (timeline)
timelinebuilder.create()
   .cyclecount(animation.indefinite)
   .keyframes(oneframe)
   .build()
   .play();

the above code snippet is really all you need to make a simple game or animation. However, you might want to take things to the next level. you might want to build a game engine that can handle sprites and state of the game world.

a

game engine

is a fancy name for a utility or library responsible for encapsulating the game world, running the game loop, handling sprites, physics, etc. the wheel when creating a 2D game from scratch. to move quickly i created a uml class diagram of a game engine design.

Below is figure 1 a class diagram of the javafx game engine.

figure 1.a javafx 2 game engine design

in figure 1 a javafx 2 game engine design, you will notice three classes a

Game universe

,

sprite manager

, and

leprechaun

. the

Game universe

The class is responsible for initializing game state, running the game loop, updating sprites, handling sprite collisions, and cleaning up. the next is the

sprite manager

class which is responsible for managing sprites by adding, removing and keeping collisions. finally, is the

leprechaun

class which is responsible for maintaining the state of an image (actor). in a 2D world, a sprite can contain the speed, rotation, scene node, or image of the object that is ultimately rendered every cycle (keyframe / frames per second).

just a little reminder on the uml notation: the plus symbol “+” indicates that a member of the class is public, the minus symbol “-” is private and the pound symbol “#” means protected.

Game universe

below is the source code implementation of the gameworld class. Click to enlarge. later you will see a class diagram illustrating a simple demo game that will extend the gameworld class.

package carlfx.gameengine;

import javafx.animation.animation;
import javafx.animation.keyframe;
import javafx.animation.timeline;
import javafx.animation.timelinebuilder;
import javafx.event.actionevent;
import javafx.event.eventhandler;
import javafx.scene.group;
import javafx.scene.scene;
import javafx.stage.stage;
import javafx.util.duration;

/**
 * this application demonstrates a javafx 2.x game loop.
 * shown below are the methods which comprise of the fundamentals to a
 * simple game loop in javafx:
*
 *  initialize() - initialize the game world.
 *  begingameloop() - creates a javafx timeline object containing the game life cycle.
 *  updatesprites() - updates the sprite objects each period (per frame)
 *  checkcollisions() - method will determine objects that collide with each other.
 *  cleanupsprites() - any sprite objects needing to be removed from play.
 *
 * @author cdea
 */
public abstract class gameworld {

    /** the javafx scene as the game surface */
    private scene gamesurface;
    /** all nodes to be displayed in the game window. */
    private group scenenodes;
    /** the game loop using javafx's timeline api.*/
    private static timeline gameloop;

    /** number of frames per second. */
    private final int framespersecond;

    /** title in the application window.*/
    private final string windowtitle;

    /**
     * the sprite manager.
     */
    private final spritemanager spritemanager = new spritemanager();

    /**
     * constructor that is called by the derived class. this will
     * set the frames per second, title, and setup the game loop.
     * @param fps - frames per second.
     * @param title - title of the application window.
     */
    public gameworld(final int fps, final string title) {
        framespersecond = fps;
        windowtitle = title;
        // create and set timeline for the game loop
        buildandsetgameloop();
    }

    /**
     * builds and sets the game loop ready to be started.
     */
    protected final void buildandsetgameloop() {

        final duration oneframeamt = duration.millis(1000/getframespersecond());
        final keyframe oneframe = new keyframe(oneframeamt,
            new eventhandler() {

                @override
                public void handle(javafx.event.actionevent event) {

                    // update actors
                    updatesprites();

                    // check for collision
                    checkcollisions();

                    // removed dead things
                    cleanupsprites();

                }
        }); // oneframe

        // sets the game world's game loop (timeline)
        setgameloop(timelinebuilder.create()
                .cyclecount(animation.indefinite)
                .keyframes(oneframe)
                .build());
    }

    /**
     * initialize the game world by update the javafx stage.
     * @param primarystage
     */
    public abstract void initialize(final stage primarystage);

    /**kicks off (plays) the timeline objects containing one key frame
     * that simply runs indefinitely with each frame invoking a method
     * to update sprite objects, check for collisions, and cleanup sprite
     * objects.
     *
     */
    public void begingameloop() {
        getgameloop().play();
    }

    /**
     * updates each game sprite in the game world. this method will
     * loop through each sprite and passing it to the handleupdate()
     * method. the derived class should override handleupdate() method.
     *
     */
    protected void updatesprites() {
        for (sprite sprite:spritemanager.getallsprites()){
            handleupdate(sprite);
        }
    }

    /** updates the sprite object's information to position on the game surface.
     * @param sprite - the sprite to update.
     */
    protected void handleupdate(sprite sprite) {
    }

    /**
     * checks each game sprite in the game world to determine a collision
     * occurred. the method will loop through each sprite and
     * passing it to the handlecollision()
     * method. the derived class should override handlecollision() method.
     *
     */
    protected void checkcollisions() {
        // check other sprite's collisions
        spritemanager.resetcollisionstocheck();
        // check each sprite against other sprite objects.
        for (sprite spritea:spritemanager.getcollisionstocheck()){
            for (sprite spriteb:spritemanager.getallsprites()){
                if (handlecollision(spritea, spriteb)) {
                    // the break helps optimize the collisions
                    //  the break statement means one object only hits another
                    // object as opposed to one hitting many objects.
                    // to be more accurate comment out the break statement.
                    break;
                }
            }
        }
    }

    /**
     * when two objects collide this method can handle the passed in sprite
     * objects. by default it returns false, meaning the objects do not
     * collide.
     * @param spritea - called from checkcollision() method to be compared.
     * @param spriteb - called from checkcollision() method to be compared.
     * @return boolean true if the objects collided, otherwise false.
     */
    protected boolean handlecollision(sprite spritea, sprite spriteb) {
        return false;
    }

    /**
     * sprites to be cleaned up.
     */
    protected void cleanupsprites() {
        spritemanager.cleanupsprites();
    }

    /**
     * returns the frames per second.
     * @return int the frames per second.
     */
    protected int getframespersecond() {
        return framespersecond;
    }

    /**
     * returns the game's window title.
     * @return string the game's window title.
     */
    public string getwindowtitle() {
        return windowtitle;
    }

    /**
     * the game loop (timeline) which is used to update, check collisions, and
     * cleanup sprite objects at every interval (fps).
     * @return timeline an animation running indefinitely representing the game
     * loop.
     */
    protected static timeline getgameloop() {
        return gameloop;
    }

    /**
     * the sets the current game loop for this game world.
     * @param gameloop timeline object of an animation running indefinitely
     * representing the game loop.
     */
    protected static void setgameloop(timeline gameloop) {
        gameworld.gameloop = gameloop;
    }

    /**
     * returns the sprite manager containing the sprite objects to
     * manipulate in the game.
     * @return spritemanager the sprite manager.
     */
    protected spritemanager getspritemanager() {
        return spritemanager;
    }

    /**
     * returns the javafx scene. this is called the game surface to
     * allow the developer to add javafx node objects onto the scene.
     * @return
     */
    public scene getgamesurface() {
        return gamesurface;
    }

    /**
     * sets the javafx scene. this is called the game surface to
     * allow the developer to add javafx node objects onto the scene.
     * @param gamesurface the main game surface (javafx scene).
     */
    protected void setgamesurface(scene gamesurface) {
        this.gamesurface = gamesurface;
    }

    /**
     * all javafx nodes which are rendered onto the game surface(scene) is
     * a javafx group object.
     * @return group the root containing many child nodes to be displayed into
     * the scene area.
     */
    public group getscenenodes() {
        return scenenodes;
    }

    /**
     * sets the javafx group that will hold all javafx nodes which are rendered
     * onto the game surface(scene) is a javafx group object.
     * @param scenenodes the root container having many children nodes
     * to be displayed into the scene area.
     */
    protected void setscenenodes(group scenenodes) {
        this.scenenodes = scenenodes;
    }

}

sprite manager

a sprite manager class is a helper class to help the game loop keep track of sprites. normally a sprite manager will contain all sprites and each sprite contains a javafx node which is displayed on the stage graph.

package carlfx.gameengine;

import java.util.*;

/**
 * sprite manager is responsible for holding all sprite objects, and cleaning up
 * sprite objects to be removed. all collections are used by the javafx
 * application thread. during each cycle (animation frame) sprite management
 * occurs. this assists the user of the api to not have to create lists to
 * later be garbage collected. should provide some performance gain.
 * @author cdea
 */
public class spritemanager {
    /** all the sprite objects currently in play */
    private final static list game_actors = new arraylist<>();

    /** a global single threaded list used to check collision against other
     * sprite objects.
     */
    private final static list check_collision_list = new arraylist<>();

    /** a global single threaded set used to cleanup or remove sprite objects
     * in play.
     */
    private final static set clean_up_sprites = new hashset<>();

    /** */
    public list getallsprites() {
        return game_actors;
    }

    /**
     * varargs of sprite objects to be added to the game.
     * @param sprites
     */
    public void addsprites(sprite... sprites) {
        game_actors.addall(arrays.aslist(sprites));
    }

    /**
     * varargs of sprite objects to be removed from the game.
     * @param sprites
     */
    public void removesprites(sprite... sprites) {
        game_actors.removeall(arrays.aslist(sprites));
    }

    /** returns a set of sprite objects to be removed from the game_actors.
     * @return clean_up_sprites
     */
    public set getspritestoberemoved() {
        return clean_up_sprites;
    }

 /**
     * adds sprite objects to be removed
     * @param sprites varargs of sprite objects.
     */
    public void addspritestoberemoved(sprite... sprites) {
        if (sprites.length > 1) {
            clean_up_sprites.addall(arrays.aslist((sprite[]) sprites));
        } else {
            clean_up_sprites.add(sprites[0]);
        }
    }

    /**
     * returns a list of sprite objects to assist in collision checks.
     * this is a temporary and is a copy of all current sprite objects
     * (copy of game_actors).
     * @return check_collision_list
     */
    public list getcollisionstocheck() {
        return check_collision_list;
    }

    /**
     * clears the list of sprite objects in the collision check collection
     * (check_collision_list).
     */
    public void resetcollisionstocheck() {
        check_collision_list.clear();
        check_collision_list.addall(game_actors);
    }

    /**
     * removes sprite objects and nodes from all
     * temporary collections such as:
     * clean_up_sprites.
     * the sprite to be removed will also be removed from the
     * list of all sprite objects called (game_actors).
     */
    public void cleanupsprites() {

        // remove from actors list
        game_actors.removeall(clean_up_sprites);

        // reset the clean up sprites
        clean_up_sprites.clear();
    }
}

leprechaun

the sprite class represents an image or a node to display on the javafx scene graph. in a 2D game, a sprite will contain additional information such as its speed for the object as it moves through the stage area. the game loop will call the update () and collide () methods at each interval of a keyframe.

package carlfx.gameengine;

import java.util.arraylist;
import java.util.list;
import javafx.animation.animation;
import javafx.scene.node;

/**
 * the sprite class represents a image or node to be displayed.
 * in a 2d game a sprite will contain a velocity for the image to
 * move across the scene area. the game loop will call the update()
 * and collide() method at every interval of a key frame. a list of
 * animations can be used during different situations in the game
 * such as rocket thrusters, walking, jumping, etc.
 * @author cdea
 */
public abstract class sprite {

    /** animation for the node */
    public list animations = new arraylist<>();

    /** current display node */
    public node node;

    /** velocity vector x direction */
    public double vx = 0;

    /** velocity vector y direction */
    public double vy = 0;

    /** dead? */
    public boolean isdead = false;

    /**
     * updates this sprite object's velocity, or animations.
     */
    public abstract void update();

    /**
     * did this sprite collide into the other sprite?
     *
     * @param other - the other sprite.
     * @return
     */
    public boolean collide(sprite other) {
        return false;
    }
}

phew! if you’ve made it this far, you are a brave soul. let’s take a little break and try out the demo I created using the game engine above.

below is a java web start button to launch the game demo. later you will see the design and source code detailing how it was created.

conditions:

  • java 7 or later

  • javafx 2.0.2 or later

  • windows xp or later (should be available soon for linux / macos)


atomsmasher game loop demo

demo

gamelooppart2 design

Below is a class diagram of the game demo called atom smasher which uses the game engine framework mentioned earlier.

below is the atom smasher class diagram in figure 2.



figure 2.atom smasher class diagram

gamelooppart2

the gamelooppart2 is the main javafx driver or application that runs the game. this creates a gameworld object to initialize and starts the game loop.

package carlfx;

import carlfx.gameengine.gameworld;
import javafx.application.application;
import javafx.stage.stage;
/**
 * the main driver of the game.
 * @author cdea
 */
public class gamelooppart2 extends application {

    gameworld gameworld = new atomsmasher(60, "javafx 2 gametutorial part 2 - game loop");
    /**
     * @param args the command line arguments
     */
    public static void main(string[] args) {
        launch(args);
    }

    @override
    public void start(stage primarystage) {
        // setup title, scene, stats, controls, and actors.
        gameworld.initialize(primarystage);

        // kick off the game loop
        gameworld.begingameloop();

        // display window
        primarystage.show();
    }

}

atom crusher

atomsmasher is a class derived from the gameworld class. it creates many spheres which come to life with random speeds. Button commands allow the user to generate more “atoms” (javafx circle nodes). As each atom collides they will invoke the implode () method which produces a fade transition animation. you will notice how easy it is to implement this game just by implementing

initialize (), handleupdate (), handlecollision (),

and

cleanupsprites ()

methods. once implemented, the game engine does the rest. the

initialize()

The method creates the button controls for the user. to update the sprite positions or change the state of the game, you will implement the

handleupdate ()

method. to compare all the sprites if they collided with each other, you will implement the

handle collision ()

. the last part of the game loop lifecycle is cleaning up the sprites. clean means update sprite manager and update javafx scene (remove nodes).

package carlfx;

import carlfx.gameengine.gameworld;
import carlfx.gameengine.sprite;
import java.util.random;
import javafx.animation.timeline;
import javafx.event.eventhandler;
import javafx.scene.group;
import javafx.scene.scene;
import javafx.scene.control.buttonbuilder;
import javafx.scene.control.label;
import javafx.scene.input.mouseevent;
import javafx.scene.layout.hboxbuilder;
import javafx.scene.layout.vbox;
import javafx.scene.layout.vboxbuilder;
import javafx.scene.paint.color;
import javafx.scene.shape.circle;
import javafx.stage.stage;
import static javafx.animation.animation.status.running;
import static javafx.animation.animation.status.stopped;

/**
 * this is a simple game world simulating a bunch of spheres looking
 * like atomic particles colliding with each other. when the game loop begins
 * the user will notice random spheres (atomic particles) floating and
 * colliding. the user is able to press a button to generate more
 * atomic particles. also, the user can freeze the game.
 *
 * @author cdea
 */
public class atomsmasher extends gameworld {
    /** read only field to show the number of sprite objects are on the field*/
    private final static label num_sprites_field = new label();

    public atomsmasher(int fps, string title){
        super(fps, title);
    }

    /**
     * initialize the game world by adding sprite objects.
     * @param primarystage
     */
    @override
    public void initialize(final stage primarystage) {
        // sets the window title
        primarystage.settitle(getwindowtitle());

        // create the scene
        setscenenodes(new group());
        setgamesurface(new scene(getscenenodes(), 640, 580));
        primarystage.setscene(getgamesurface());

        // create many spheres
        generatemanyspheres(150);

        // display the number of spheres visible.
        // create a button to add more spheres.
        // create a button to freeze the game loop.
        final timeline gameloop = getgameloop();
        vbox stats = vboxbuilder.create()
            .spacing(5)
            .translatex(10)
            .translatey(10)
            .children(hboxbuilder.create()
                .spacing(5)
                .children(new label("number of particles: "), // show no. particles
                    num_sprites_field).build(),

                    // button to build more spheres
                    buttonbuilder.create()
                        .text("regenerate")
                        .onmousepressed(new eventhandler() {
                            @override
                            public void handle(mouseevent arg0) {
                                generatemanyspheres(150);
                            }}).build(),

                    // button to freeze game loop
                    buttonbuilder.create()
                        .text("freeze/resume")
                        .onmousepressed(new eventhandler() {

                            @override
                            public void handle(mouseevent arg0) {
                                switch (gameloop.getstatus()) {
                                    case running:
                                        gameloop.stop();
                                        break;
                                    case stopped:
                                        gameloop.play();
                                        break;
                                }
                            }}).build()
            ).build(); // (vbox) stats on children

        // lay down the controls
        getscenenodes().getchildren().add(stats);
    }

    /**
     * make some more space spheres (atomic particles)
     */
    private void generatemanyspheres(int numspheres) {
        random rnd = new random();
        scene gamesurface = getgamesurface();
        for (int i=0; i (gamesurface.getwidth() - (circle.getradius() * 2))) {
                newx = gamesurface.getwidth() - (circle.getradius()  * 2);
            }

            // check for the bottom of screen the height newy is greater than height
            // minus radius times 2(height of sprite)
            double newy = rnd.nextint((int) gamesurface.getheight());
            if (newy > (gamesurface.getheight() - (circle.getradius() * 2))) {
                newy = gamesurface.getheight() - (circle.getradius() * 2);
            }

            circle.settranslatex(newx);
            circle.settranslatey(newy);
            circle.setvisible(true);
            circle.setid(b.tostring());

            // add to actors in play (sprite objects)
            getspritemanager().addsprites(b);

            // add sprite's
            getscenenodes().getchildren().add(0, b.node);

        }
    }

    /**
     * each sprite will update it's velocity and bounce off wall borders.
     * @param sprite - an atomic particle (a sphere).
     */
    @override
    protected void handleupdate(sprite sprite) {
        if (sprite instanceof atom) {
            atom sphere = (atom) sprite;

            // advance the spheres velocity
            sphere.update();

            // bounce off the walls when outside of boundaries
            if (sphere.node.gettranslatex() > (getgamesurface().getwidth()  -
                sphere.node.getboundsinparent().getwidth()) ||
                sphere.node.gettranslatex() < 0 ) {                 sphere.vx = sphere.vx * -1;             }             if (sphere.node.gettranslatey() > getgamesurface().getheight()-
                sphere.node.getboundsinparent().getheight() ||
                sphere.node.gettranslatey() < 0) {
                sphere.vy = sphere.vy * -1;
            }
        }
    }

    /**
     * how to handle the collision of two sprite objects. stops the particle
     * by zeroing out the velocity if a collision occurred.
     * @param spritea
     * @param spriteb
     * @return
     */
    @override
    protected boolean handlecollision(sprite spritea, sprite spriteb) {
        if (spritea.collide(spriteb)) {
            ((atom)spritea).implode(this);
            ((atom)spriteb).implode(this);
            getspritemanager().addspritestoberemoved(spritea, spriteb);
            return true;
        }
        return false;
    }

    /**
     * remove dead things.
     */
    @override
    protected void cleanupsprites() {
        // removes from the scene and backend store
        super.cleanupsprites();

        // let user know how many sprites are showing.
        num_sprites_field.settext(string.valueof(getspritemanager().getallsprites().size()));

    }
}

atom

the atom class extends from the sprite class. an atom is a sprite that appears as a spherical object that moves across the stage. an atom will have a random radius, color and speed. as each atom sprite collides with another atom, they will animate a fade transition (the implode () method).

package carlfx;

import carlfx.gameengine.gameworld;
import carlfx.gameengine.sprite;
import javafx.animation.fadetransitionbuilder;
import javafx.event.actionevent;
import javafx.event.eventhandler;
import javafx.scene.paint.color;
import javafx.scene.paint.radialgradient;
import javafx.scene.paint.radialgradientbuilder;
import javafx.scene.paint.stop;
import javafx.scene.shape.circle;
import javafx.scene.shape.circlebuilder;
import javafx.util.duration;

/**
 * a spherical looking object (atom) with a random radius, color, and velocity.
 * when two atoms collide each will fade and become removed from the scene. the
 * method called implode() implements a fade transition effect.
 *
 * @author cdea
 */
public class atom extends sprite {

    public atom(double radius, color fill) {
        circle sphere = circlebuilder.create()
                .centerx(radius)
                .centery(radius)
                .radius(radius)
                .cache(true)
                .build();

        radialgradient rgrad = radialgradientbuilder.create()
                    .centerx(sphere.getcenterx() - sphere.getradius() / 3)
                    .centery(sphere.getcentery() - sphere.getradius() / 3)
                    .radius(sphere.getradius())
                    .proportional(false)
                    .stops(new stop(0.0, fill), new stop(1.0, color.black))
                    .build();

        sphere.setfill(rgrad);

        // set javafx node to a circle
        node = sphere;

    }

    /**
     * change the velocity of the atom particle.
     */
    @override
    public void update() {
        node.settranslatex(node.gettranslatex() + vx);
        node.settranslatey(node.gettranslatey() + vy);
    }

    @override
    public boolean collide(sprite other) {
        if (other instanceof atom) {
            return collide((atom)other);
        }
       return false;
    }

    /**
     * when encountering another atom to determine if they collided.
     * @param other another atom
     * @return boolean true if this atom and other atom has collided,
     * otherwise false.
     */
    private boolean collide(atom other) {

        // if an object is hidden they didn't collide.
        if (!node.isvisible() ||
            !other.node.isvisible() ||
            this == other) {
            return false;
        }

        // determine it's size
        circle othersphere = other.getascircle();
        circle thissphere =  getascircle();
        double dx = othersphere.gettranslatex() - thissphere.gettranslatex();
        double dy = othersphere.gettranslatey() - thissphere.gettranslatey();
        double distance = math.sqrt( dx * dx + dy * dy );
        double mindist  = othersphere.getradius() + thissphere.getradius() + 3;

        return (distance < mindist);
    }

    /**
     * returns a node casted as a javafx circle shape.
     * @return circle shape representing javafx node for convenience.
     */
    public circle getascircle() {
        return (circle) node;
    }

    /**
     * animate an implosion. once done remove from the game world
     * @param gameworld - game world
     */
    public void implode(final gameworld gameworld) {
        vx = vy = 0;
        fadetransitionbuilder.create()
            .node(node)
            .duration(duration.millis(300))
            .fromvalue(node.getopacity())
            .tovalue(0)
            .onfinished(new eventhandler() {
                @override
                public void handle(actionevent arg0) {
                    isdead = true;
                    gameworld.getscenenodes().getchildren().remove(node);
                }
            })
            .build()
            .play();
    }
}

hope you have the chance to understand the fundamentals of a game loop and apply the knowledge later by implementing a robust game engine. Although, I briefly mention collision, I’m saving that for part 4 of these tutorials. please stay tuned for part 3 where we will get into typing using the keyboard or mouse. feel free to experiment. let me know what you have on offer.
;-)

carl

crts



http://en.wikipedia.org/wiki/refresh_rate

double buffer



http://en.wikipedia.org/wiki/double_buffering#double_buffering

develop games in java

by david brackeen with bret barker and laurence vanhelsuwe. page 56 “get rid of flicker and tears”

buffer strategy

API –

http://docs.oracle.com/javase/7/docs/api/java/awt/image/bufferstrategy.html


Source link