More aboutIsobar
CloseX

Intro to Android Game Programming

Intro to Android Game Programming

Flixel is an Actionscript-based game framework developed by Adam “Atomic” Saltsman http://flixel.org/index.html.

Due to the tremendous work of Wing Eraser a Java-based port has been created http://code.google.com/p/flixel-android/, which closely resemeble the AS3-based Flixel in programming paradigm.

In this tutorial, we will be creating a simple jumper game, containing a few entities, a droid, and pusher and a few elevators. Each entity is declared as a separate class containing it’s own asset resources, and listeners for digital touchpad events.

File: Main.java
[source language="java"]
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

// ORIENTATION
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setContentView(new GameView(this, R.class));
}
}
[/source]

File: GameView.java
[source language="java"]
import org.flixel.FlxGame;
import org.flixel.FlxGameView;
import android.content.Context;

public class GameView extends FlxGameView
{
public GameView(Context context, Class resource)
{
super(new FlxGame(400, 240, SimpleJumper.class, context, resource), context);
}
}
[/source]

File: Droid.java
[source language="java"]

import org.flixel.FlxG;
import org.flixel.FlxSound;
import org.flixel.FlxSprite;

public class Droid extends FlxSprite
{
private final FlxSound sound = new FlxSound();

public Droid(int X, int Y)
{
super(X, Y);
loadGraphic(R.drawable.player, true, true);
maxVelocity.x = 100; // walking speed
acceleration.y = 400; // gravity
drag.x = maxVelocity.x * 4; // deceleration (sliding to a stop)

// tweak the bounding box for better feel
width = 8;
height = 10;

offset.x = 3;
offset.y = 3;

addAnimation(“idle”, new int[] { 0 }, 0, false);
addAnimation(“walk”, new int[] { 1, 2, 3, 0 }, 12);
addAnimation(“walk_back”, new int[] { 3, 2, 1, 0 }, 10, true);
addAnimation(“flail”, new int[] { 1, 2, 3, 0 }, 18, true);
addAnimation(“jump”, new int[] { 4 }, 0, false);
}

@Override
public void update()
{
// Smooth slidey walking controls
acceleration.x = 0;
if (FlxG.dpad.pressed(“LEFT”)) acceleration.x -= drag.x;
if (FlxG.dpad.pressed(“RIGHT”)) acceleration.x += drag.x;

if (onFloor)
{
// Jump controls
if (FlxG.dpad.justTouched(“UP”))
{
sound.loadEmbedded(R.raw.jump);
sound.play();

velocity.y = -acceleration.y * 0.51f;
play(“jump”);

}// Animations
else if (velocity.x > 0)
{
play(“walk”);
}
else if (velocity.x < 0)
{
play("walk_back");
}
else play("idle");
}
else if (velocity.y < 0) play("jump");
else play("flail");

// Default object physics update
super.update();
}

}

[/source]