Showing posts with label flashdev. Show all posts
Showing posts with label flashdev. 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.

Tuesday, February 24, 2009

Flash Kraken - Complete!

Okay, goal #1 for Open Source Flash development is complete.
Behold a single SWF file that shows a picture and plays a song when you click the play button. It will take a minute to load, since it's 6MB or so.

I used swfmill to capture the assets in a swf file, then compiled onto that using the FAME combo. Big thanks go to Kevin of http://open-actionscript.blogspot.com/ for providing the resources for the button logic. Flash can be a little esoteric for the traditional C programmer sometimes.

Source, including assets and xml file for swfmill here.

Friday, January 16, 2009

Flash Kraken

Behold my first app written in open source Flash development tools.

Okay, it's not much but it demonstrates a few things I wanted to do, namely a progress bar that fills up as the image loads. The image is located remotely, and I need to figure out how to make libraries of resources that can be embedded in the flash app.

Project source: CTD.zip

The name of the zip and the swf file indicate my intended first game in flash, Coin Toss Dungeon. Coin Toss Dungeon references a "What's New with Phil and Dixie" cartoon from the early 80's that I originally read in Dragon magazine in my greasier teenage years. Through the magic of the interwebs (and Mr. Foglio's generosity) I present it for you here.

Wednesday, October 29, 2008

Open Source Flash Development

Who says this blog isn't multi-interest?

I have decided to do some Macromedia Flash development, and in my usual frugal vein I am using open source tools. Here is what I am using:

  • Eclipse IDE v3.3.2 (Europa)
  • Motion-Twin ActionScript 2 compiler (v1.14)
  • ActionScript Development Kit (ASDT)
  • Flashout (Eclipse Plugin) (v0.2.1.6)
Making this all work together is all nicely written up on the communitymx website here.

I encountered some problems, mostly version based, with getting the HelloWorld example to work.
  • The "New Folder" right-click command is apparently not enabled by default for the ActionScript perspective in Eclipse 3.3.2. To fix this, customize the perspective, choose the "General" shortcut category and check "folder" (and probably also "file".)
  • The "core" linked directory in the example will not be created. A newer version of Motion Twin uses "std8" instead. No biggie, but a difference.
  • And speaking of std8, the Eclipse preferences for Flashout want to know the "Directory of Macromedia's core classes." Give it the sdt8 subdir of the Motion Twin installation dir.
  • The Flashout.as file required for compilation contains code that is not compatible with the newer version of Motion Twin. The "TRACE" function is now "trace", and no I don't know why. Open Flashout.as with a text editor and replace every instance of TRACE with trace. I recommend Wordpad if you use windows, since it has a replace function and treats the carriage returns in the file with respect, unlike notepad. For future covenience (read: laziness) I put it in the std8 directory in the Motion Twin install dir.
One issue I have not gotten to work is the suggestion menus for the Flashout class while editing the ActionScript class files. I'm afraid that when I move to multiple class files that one won't recognize the classes in another. It seems to compile and work okay, but it would be nice if the compiler gave some indication that it knows what I'm talking about. Yes, I've tried putting it in the source directories for the project.