Skip to content

Code Snippets

Almas Baimagambetov edited this page Oct 13, 2016 · 15 revisions

This page contains various useful code snippets. Note that if you are calling FXGL. methods within your GameApplication class, then in most cases FXGL. can be omitted (getAssetLoader() instead of FXGL.getAssetLoader()).

Scrolling background image

Texture texture = FXGL.getAssetLoader().loadTexture("background.png");
ScrollingBackgroundView bg = new ScrollingBackgroundView(texture, Orientation.HORIZONTAL);

getGameScene().addGameView(bg);

Looping background music

Music music = FXGL.getAssetLoader().loadMusic("music.mp3");
music.setCycleCount(Integer.MAX_VALUE);

FXGL.getAudioPlayer().playMusic(music);

Passing references around in your game

Say you have an A* grid object in your MyApp that you want to access from some AIControl. Have a public accessor to return your grid object in MyApp:

public AStarGrid getGrid() {}

Then, in AIControl, you can obtain the reference to your app and typecast it to your app type:

MyApp app = (MyApp) FXGL.getApp();
app.getGrid() ... // do stuff with the grid

Clone this wiki locally