The framework allows the development of one abstracted java game code using its Layers, Sounds APIs and the jbox2d as the physics engine, while keeping the Game Loop concepts.
Here we go into the details of how to get your game up and running using PlayN:
Note: previous maven experience would be of great help. PlayN requires Maven 3.x
Follow the Getting Started wiki page. NOTE: you need to add android properties of the version and sdk path to the main maven pom file file playn/pom.xml in the <properties> tag
I recommend developing on eclipse using the m2eclipse (maven to eclipse) plugin, Google Plugin for Eclipse and Android Plugin for Eclipse and importing the playn folder as a maven project. (This will import many playn-* projects)
For new project creation with jbox2d physics engine enabled, I have been unsuccessful to generate a new project with jbox2d dependency from maven or eclipse using the playn architecture type.
So, I copied the showcase sample to mygame folder and changed the <artifactId> and <name> in the mygame/pom.xml to:
and in the nested projects (core, html, flash, android) changing the <parent>, <artifactId> and <name> in their pom.xml files.
After finishing the development of the game on the JVM, you would want to test it against HTML, Flash and Android.
I tried and succeeded testing against JVM, HTML and Android.
Some commands I frequently use (on the directory of the project):
- ant run-java (Runs the game on the JVM)
- ant run-devmode (Runs the game in the GWT development mode, so we can see the logs and exceptions, if any. However the frame-rate is extremely low for real testing).
- ant run-html (Compiles the code using GWT for production).
- ant clean && mvn clean (clean the project files).
Steps followed to get your Android version of the game to work:
First, I followed the Android Ports wiki page of playn. Again, didn’t seem to be working, as dependencies weren’t found. I followed these steps to create an Android project
- Create new Android project with 2.3.3 as the target sdk. (e.g. mygame-android).
- In your AndroidManifest.xml, put in the <manifest> tag:
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
and in the <activity> tag:
And in the activity
Leave the syntax errors for now, we will fix them later.
- $ cd mygame-android
- $ mkdir lib
- $ cp playn/android/target/playn-android-1.0-SNAPSHOT.jar lib/
- $ cp playn/java/target/playn-java-1.0-SNAPSHOT.jar lib/
- $ cp playn/android/lib/pythagoras-1.1-SNAPSHOT.jar lib/
- $ cp -rf playn/android/libs .
- Open mygame-android project’s Properties. If not, click on the Java Build Path selection. Under the Sources tab, you should already have two sources: mygame-src and mygame-res. In addition to this, you will need to add a Linked Source to the playn-android source. Hit the Linked Source… button and browse to the .playn/android/src folder for the Linked Folder Location. Call the Folder Name playn-android-src or something simliar.
- Do step 8 again: link playn/core/src to playn-core-src and link mygame/src to mygame-src.
- In your mygame folder create new folder called “assets” to hold the resources your game needs. (OpenGL needs 3 asset files we will copy soon).
- $ mkdir -p mygame-android/assets/playn/android/
- $ mkdir -p mygame-android/assets/playn/showcase
- $ cp playn/android/src/playn/android/tex-fragment-shader.txt mygame-android/assets/playn/android/
- $ cp playn/android/src/playn/android/vertex-shader.txt mygame-android/assets/playn/android/
- $ cp playn/android/src/playn/android/color-fragment-shader.txt mygame-android/assets/playn/android/
- Link to your resources in the path stated above in your activity playn/showcase/resources:
$ ln -s mygame/core/src/playn/showcase/resources mygame-android/assets/playn/showcase/resources
Tips and tricks:
- Avoid using either java.util.Timer and java.util.TimerTask or GWT’s single threaded timer into your game, If you really need Timer, you need to do your own Timer and updating it in your update method to trigger events.
- Avoid references to device-specific (or GWT-only) classes in your game code.
- Make sure you plan your controls around different input schemes. (e.g. most Android games shouldn’t be working with keyboard input).
Authored by: Mostafa Eweda @MostafaEweda