Archive for March, 2014

Animations in OpenFL

Saturday, March 15th, 2014

Click on the image and use the arrow keys to control the character.
Fallback or ‘alternate’ content goes here. This content will only be visible if the SWF fails to load.

I am experimenting with OpenFL. Very fast and powerful when developing Flash apps.


import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import openfl.Assets;

class Player extends Sprite {

var bitmapArray : Array;

var i : Int= 0;

var moving_up : Bool = false;
var moving_down : Bool = false;
var moving_left : Bool = false;
var moving_right : Bool = false;

public function new () {

super ();
initialize();
}

public function initialize() {
bitmapArray = new Array();

var bitmap : Bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player00.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player10.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player11.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player12.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player13.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player14.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player15.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player16.png");
bitmapArray.push(bitmap);
bitmap = new Bitmap();
bitmap.bitmapData = Assets.getBitmapData ("assets/images/player17.png");
bitmapArray.push(bitmap);

addChild(bitmapArray[i]);

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(evt:Event) {

if ( moving() ) {
removeChild(bitmapArray[i]);

i = (i + 1) % 9;

addChild(bitmapArray[i]);
}

if ( moving_up ) {
y = y - 1;
}

if ( moving_down ) {
y = y + 1;
}

if ( moving_left ) {
x = x - 1;
}

if ( moving_right ) {
x = x + 1;
}
}

public function moving() : Bool {
return (moving_up || moving_down || moving_left || moving_right);
}

public function up() {
moving_up = true;

}

public function down() {
moving_down = true;

}

public function left() {
moving_left = true;

}

public function right() {
moving_right = true;

}

public function stop() {
moving_up = false;
moving_down = false;
moving_left = false;
moving_right = false;
}

}