Pages

Wednesday, July 6, 2011

Andengine application with Scala

Let's take a look at how to make a simple application with Scala and Andengine. This is the quick and dirty conversion from Andengine ParticleSystemSimpleExample made with Java.

Folders
Folder paths are only for example and you can change them from the pom.xml.

/src/main/resources/ - all none code stuff
/src/main/resources/assets/ - folder that contains all loadable content
/src/main/scala - all code
/src/main/android -all android specific stuff (configured in the pom.xml)


You can get the Android specific content to the /src/main/android folder by creating an empty Android module with IntelliJ Idea. Then copy AndroidManifest.xml and the res folder from the generated module to the /src/main/android folder in your Maven project.

Android config
Set the executable applications package as well as the executable file name in AndroidManifest.xml

/src/main/android/AndroidManifest.xml
Give a proper name to your application
/src/main/android/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ParticleSystemSimple</string>
</resources>


Code

This is the good stuff, finally!

First create a proper package by right clicking the /src/main/scala folder


Next, create the Scala class. Right click the newly created package and select new -> Scala Class

package com.studfarm.example

import com.studfarm.example.ParticleSystemSimpleExample._

ParticleSystemSimpleExample {}



Create a companion object for the class. Companion objects are used to store all static methods and variables. An object is called a companion object when the object is in the same file with the class and has the same name as the class.


Add these to the companion object:


val CAMERA_WIDTH:Int = 720

val CAMERA_HEIGHT:Int = 480

Remember to import the companion object so you can use the static methods and variables more easily.

package com.studfarm.example

import com.studfarm.example.ParticleSystemSimpleExample._

class ParticleSystemSimpleExample {

}

object ParticleSystemSimpleExample {
val CAMERA_WIDTH:Int = 720
val CAMERA_HEIGHT:Int = 480
}

Next we need to inherit the BaseGameActivity. Add extends BaseGameActivity to your class and accept the import. Implement the following methods:
  • onLoadEngine:Engine
  • onLoadResources
  • onLoadScene:Scene
  • onLoadComplete
The easiest way to do the implementation is to right click the code region of the class and select generate (or just alt+insert) -> implement methods... -> select the methods.

Unfortunately the result is not clean and you'll need to add an override keyword in front of every def generated.

package com.studfarm.example

import org.anddev.andengine.ui.activity.BaseGameActivity

class ParticleSystemSimpleExample extends BaseGameActivity{

override def onLoadScene() = null

override def onLoadResources() {}

override def onLoadEngine() = null

override def onLoadComplete() {}
}

object ParticleSystemSimpleExample {

}
Next we'll implement the methods.

OnLoadEngine

When the program is loaded, show a text to the user and set the camera.

override def onLoadEngine:Engine= {
Toast.makeText(this, "Touch the screen to move the particlesystem.", Toast.LENGTH_LONG).show
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT)
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
onLoadResources

Loads all resources for the application/game

override def onLoadResources {
this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mParticleTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/particle_point.png", 0, 0);
this.mEngine.getTextureManager().loadTexture(this.mTexture);
}
particle_point.png is placed under /src/main/resources/assets/gfx/ folder

Create the Scene:
override def onLoadScene:Scene= {
this.mEngine.registerUpdateHandler(new FPSLogger());

val scene = new Scene(1);
val particleEmitter = new CircleOutlineParticleEmitter(CAMERA_WIDTH * 0.5f, CAMERA_HEIGHT * 0.5f + 20, 80);
val particleSystem = new ParticleSystem(particleEmitter, 60, 60, 360, this.mParticleTextureRegion);

scene.setOnSceneTouchListener(new IOnSceneTouchListener() {

override def onSceneTouchEvent(pScene:Scene, pSceneTouchEvent:TouchEvent):Boolean= {
particleEmitter.setCenter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
true;
}

particleSystem.addParticleInitializer(new ColorInitializer(1, 0, 0))
particleSystem.addParticleInitializer(new AlphaInitializer(0))
particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE)
particleSystem.addParticleInitializer(new VelocityInitializer(-2, 2, -20, -10))
particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f))

particleSystem.addParticleModifier(new ScaleModifier(1.0f, 2.0f, 0, 5))
particleSystem.addParticleModifier(new ColorModifier(1, 1, 0, 0.5f, 0, 0, 0, 3))
particleSystem.addParticleModifier(new ColorModifier(1, 1, 0.5f, 1, 0, 1, 4, 6))
particleSystem.addParticleModifier(new AlphaModifier(0, 1, 0, 1))
particleSystem.addParticleModifier(new AlphaModifier(1, 0, 5, 6))
particleSystem.addParticleModifier(new ExpireModifier(6, 6))

scene.getLastChild().attachChild(particleSystem)
});

return scene;
}
All done. Run the mvn clean package. You should now have a working application. You can test it with the Android emulator or with a real Android phone.


No comments:

Post a Comment