Showing posts with label haxe. Show all posts
Showing posts with label haxe. Show all posts

Friday, November 22, 2013

Arbitrary Center Bitmap Rotation in OpenFL

The problem: Bitmap objects (and display objects in general) are always rotated about the (0,0) relative coordinate when the .rotation member is set. This does not allow an object to pivot on the spot, but rather orbit its upper left point.

The solution: specify a coordinate pair to act as the center, perform a matrix rotate transformation on the point to determine where it would be after the rotation and then translate the Bitmap object to account for the difference.

The downside: the x and y location of the rotating bitmap class must be tracked separately from the x and y of the parent Bitmap, as they will be different. Also, an update method must be called to do the offset.

// import the usual suspects
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.events.Event;
import openfl.Assets;
import flash.Lib;
import flash.geom.Matrix;

class Main extends Sprite {

    private var myTest:RotatingBitmap;

    public function new() {
        super();
        myTest = new RotatingBitmap(
                   Assets.getBitmapData("assets/circle.png")
        );
        myTest.xloc = 400;
        myTest.yloc = 400;
        myTest.xcenter=25;
        myTest.ycenter=25;

        removeEventListener (Event.ENTER_FRAME, this_onEnterFrame);

addEventListener (Event.ENTER_FRAME, this_onEnterFrame);

    }

    private function this_onEnterFrame(event.Event):Void {
        myTest.rotation+=15;
        myTest.update();
    }    

}

class RotatingBitmap extends Bitmap {
        // the intended location, since x and y will be changing
public var xloc:Float;
public var yloc:Float;
        // the center x,y of the rotation
public var xcenter:Float;
public var ycenter:Float;

public function new(asset:Dynamic) {
                // load the asset and add to the stage
super(asset);
flash.Lib.current.addChild(this);
}

public function update() {
                // here's where the magic happens

                // define the rotation parms
var mymatrix:Matrix = new Matrix();
mymatrix.rotate(Math.PI*(this.rotation)/180);

                // create a point to represent the center
var mypoint = new flash.geom.Point(xcenter,ycenter);

                // transform into a new point
var newpoint:flash.geom.Point = mymatrix.transformPoint(mypoint);
                // move the bitmap to the proper new location
                this.x = xloc-newpoint.x;
this.y = yloc-newpoint.y;

}

}

Here is a little example of the default rotation and a rotation set at the (nearly) center of the image.

Monday, November 18, 2013

Dungeons and Dragsters

Despite it being mid/late November, I present my October Challenge submission, D&D. You race through the dungeon and smash into the dragon at the end. Go fast enough and defeat him, or ... not. How did this particular mash-up come about? I started with a concept I had been noodling over for a while, but as the deadline crept closer and closer I jokingly said, "I should just abandon the whole thing and remake Activision Dragster for the 2600." Somewhere in between what I had already done and the game I played as a youngster, Dungeons and Dragsters was born.

It took my coworkers approximately 60 seconds to figure out how to break the driving to get max velocity. Time to rework some of that gearing code.

Sunday, October 06, 2013

October Challenge

A friend sent me a link to this Ludum Dare mini-challenge. The challenge is to get a video game to market and make at least $1 on it in the month of October. That's it. Any genre, any platform. He even said he had $1 that was designated to buy whatever I published as incentive (a "kick-in-the-ass-starter" as he put it.)

All I can say is......

Thursday, September 12, 2013

Installing openfl and haxe 3.0 on Ubuntu

Making the jump from using NME and haxe for cross platform development to using openfl led to a bit of a frustration. Trying to upgrade to haxe 3.0 and get openfl to install caused collisions between the old version and the new version. Especially frustrating was running  sudo haxelib run openfl setup and getting the the dreaded:
Called from ? line 1
Called from RunScript.hx line 798
Called from helpers/PathHelper.hx line 170
Called from C:\HaxeToolkit\haxe/std/neko/_std/sys/io/Process.hx line 88
Called from C:\HaxeToolkit\haxe/std/neko/_std/sys/io/Process.hx line 96
Uncaught exception - Process creation failure : haxelib
To correct all the issues, I did the following:
  1. remove every instance of haxe/neko/openfl you can find from your computer. Most of it should be symlinks in /usr/bin and files in /usr/lib/(haxe|neko)
  2. go to http://www.openfl.org/download/ and download the Linux installer from the "install haxe" step. 
  3. Unpack the tar.gz file you just downloaded run the install-haxe.sh script in a console. It will fail on the last step because haxelib isn't anywhere in the path.
  4. type ln -s /usr/lib/haxe/std/tools/haxelib/haxelib haxelib to create a symbolic link to the haxelib tool in a place where it can actually be found.
  5. continue with the setup instructions and things should work from there.
After the installation, create a directory to test the samples, then use openfl create DisplayingABitmap in that directory to download the bitmap sample. Run the sample with openfl test flash and you should see a flash window (or browser) open with the openfl logo. If, like on my machine, x-shockwave-flash mime type was associated with totem (a video player) you can change that by editing /usr/share/applications/defaults.list and replace target for application/x-shockwave-flash to point to a desktop file for a flash player (I use flashplayerdebugger.desktop.) Once that is changed, openfl test flash will open the flash player correctly.