Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Building an Android Game Using flixel-android

Published? true
FormatLanguage: WikiFormat

Problem:

Game design

Solution:

Flixel is an Actionscript-based game framework developed by Adam ("Atomic") Saltsman [1].

Due to the tremendous work of Wing Eraser a Java-based port has been created [2], which closely resembles 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 its own asset resources, and listeners for digital touchpad events.

Discussion:

File: Main.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));
      }
  }

File: GameView.java

import org.flixel.FlxGame;
import org.flixel.FlxGameView;
import android.content.Context;

public class GameView extends FlxGameView
  {
    public GameView(Context context, Class<? extends Object> resource)
      {
        super(new FlxGame(400, 240, SimpleJumper.class, context, resource), context);
      }
  }

File: Droid.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();
      }

  }

Download:

The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory SimpleJumper.