Skip to content

Code Snippets

Almas Baimagambetov edited this page Aug 1, 2016 · 15 revisions

This page contains various useful code snippets.

FXGL-ready POM

The POM file below configures a typical FXGL game. All you need to do is replace ALL CAPITALS with your own details. Also make sure that you replace the FXGL version with latest. By executing

mvn package

the game will be compiled and packaged along with assets into a single executable jar.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.USERNAME</groupId>
    <artifactId>GAMENAME</artifactId>
    <version>GAMEVERSION</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <source.version>1.8</source.version>

        <!-- plugins -->
        <maven.compiler.version>3.3</maven.compiler.version>
        <maven.shade.version>2.4.2</maven.shade.version>

        <fxgl.version>0.2.4</fxgl.version>
    </properties>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.AlmasB</groupId>
            <artifactId>FXGL</artifactId>
            <version>${fxgl.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <source>${source.version}</source>
                    <target>${source.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>${maven.shade.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>PACKAGE.MAINCLASSNAME</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

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