Over the past couple of years of doing AS3 coding, it has frustrated me to no end as to why I couldn’t create a new Bitmap object and add Mouse Event Listeners to it.  As I first got into optimizing AS3 display code, trying to shy away from MovieClips everywhere, I started using Sprites more.  As I started learning about the efficiency and pixel-level control of the Bitmap/BitmapData class, I started using those classes more and more.  And every once in a while, I would want to add Mouse Event Listeners to a Bitmap.  Every time during those every once in a while times… it never worked.

I looked at LiveDocs for the Bitmap class.  Was Adobe blind??  Clearly right here:

it showed in the Bitmap class that you could use addEventListener! Instead of figuring out Why, so that I’d know in the future, I would always use a Sprite instead and go on about my happy nerd coding. Today, however, I ran into the same issue and wanted to know Why.

While both Sprite and Bitmap DO allow you to use addEventListener(), it boils down to class heirarchy, which classes each inherit from, and which Events you’re allowed to listen for in each class.  

A quick comparison:

The key to this whole Noob Mystery is highlighted in yellow.  While you Can use addEventListener() with Bitmaps, you can only listen for the following Events:

ah… whoops.

Here is an example class if you need both Bitmap/BitmapData’s pixel-level control, and Interactive Mouse/Keyboard events.

package zf  {
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.Sprite;
   import flash.events.MouseEvent;

   public class InteractiveBitmap extends Sprite  {
      private var bd:BitmapData;
      private var b:Bitmap;

      public function InteractiveBitmap( bData:BitmapData )  {
         bd = bData;
         b = new Bitmap( bd );
         addChild( b );
         addEventListener( MouseEvent.MOUSE_OVER , onMouseOverHandler , false , 0 , true );
      }

      private function onMouseOverHandler( evt:MouseEvent ):void  {
         trace( "MouseOver" );
         // insert code to do whatever to the bitmap
      }
   }
}

The only scenario that causes me trouble still is if I’m using a .png image or an image with some alpha transparency. The MOUSE_OVER event fires when you mouse over ANY part of the Sprite, not specifically the Bitmap. However, just off the top of my head, I’m sure I could whip up something MacGuyver-style using the mouse X/Y coordinates and hitTestPoint on the Bitmap to find exactly when we’re over the Bitmap.

Tags:

7
May

AS3 External XML Animation Scripting with TweenMax

   Posted by: Haelix   in Blog

Continuing my foray into preparing for collaborative Flash Game Developement, I’ve been thinking a lot about tools, how to set the project up to allow for a maximum variety of skill levels and skill sets to collaborate on the project while trying to keep Flash and Actionscript as much removed as possible.  

In a recent post, I mentioned 3 things I had learned so far about collaborative game dev.  During one of my first “test runs” of the engine ( aka.. one of the ‘more successful failures’ ) I had realized that with a number of people working on the project, most of which have no idea what the inside of Flash looks like.  Which is great!  We all have our strengths, and because they dont know Flash, it just leaves more room for things that *I* dont know.  

The Challenge: 

As the only coder in the little group, my goal is to remove Flash, AS3 and any other technical programs or bottlenecks from as much of the process as I can.  I want to try to remove dependency upon me in the situation, the need for people to come looking for me when they want something changed.  

So I had mentioned earlier that I wanted to externally load (at least now for development ) all assets… Music, Images, xml files containing map and level tile values, etc.  So once you have all of those assets loading in dynamically,  

Thats great, but step ahead to thinking about the intro of your game?  Or the title screen?  what happens to these buttons and sprites and images you’ve posted up when they’re clicked on?  Do they move?  Fade out?  How does your game “feel”?  If this responsibility falls to the designer with no flash experience…?

XML provides the perfect way for them to be able to enter values and control animations by simply editing the text/xml file.  You, as the developer, can easily get XML data and parse through it.  It’s the perfect middle ground.  Now, how do you implement this ‘perfect middle ground?’

Sample XML File:

<animation>
	<info>
		<id>tower</id>
	</info>
	<move>
		<x>100</x>
		<y>100</y>
		<time>2</time>
		<alpha>1</alpha>
		<rotation>0</rotation>
	</move>
</animation>

This is the xml file in the sourcecode.  Ideally, if I keep playing with this idea, this will obviously become a bit more filled out and structured. But for the test here, this xml data is loaded in. You may need to refresh the page if you didn’t see the little image move.

So this is essentially where this test came from… a way to put all of the animation code into an XML file so artists and designers can change values easier.

– Demo

– Source .Zip

28
Apr

AS3 Date Class, PHP and Unix Timestamps

   Posted by: Haelix   in Blog

This is a real quick post about Unix Timestamps, PHP, and ActionScript 3’s Date class.  When creating a new Date instance, you can pass in a Unix Timestamp into the Date constructor.


var myDate:Date = new Date( unix_timestamp_from_php );
trace( myDate.toString() );  // Invalid Date

When I was working with this, I continually received Invalid date in my output window.  Looking into the matter, if you read php.net’s time() function description and then taking a look at livedocs Date class we’ll quickly see the problem.

From php.net’s time() description:

time

(PHP 4, PHP 5)

time — Return current Unix timestamp

Description

int time ( void )

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

 

And then from livedoc’s Date() Class:

Date() Constructor    

Constructs a new Date object that holds the specified date and time.

The Date() constructor takes up to seven parameters (year, month, …, millisecond) to specify a date and time to the millisecond. The date that the newly constructed Date object contains depends on the number, and data type, of arguments passed.

 

So PHP time() gives you a Unix Timestamp for the current time in Seconds, and AS3 wants a time in MiliSeconds.

Here’s the fix:


var myDate:Date = new Date( unix_timestamp_from_php * 1000 );
trace( myDate.toString() ); // Tue Apr 28 13:09:32 GMT-0400 2009

OR You could also simply typecast “unix_timestamp_from_php” to type Number() like so:


var myDate:Date = new Date( Number( unix_timestamp_from_php ) );
trace( myDate.toString() ); // Tue Apr 28 13:09:32 GMT-0400 2009

Both of these work, I’m not 100% sure what happens in the Date constructor, but reading further in the Date livedocs, two bulletpoints stand out as to Why sending a String and a Number value give different results:

  • If you pass one argument of data type Number, the Date object is assigned a time value based on the number of milliseconds since January 1, 1970 0:00:000 GMT, as specified by the lone argument.
  • If you pass one argument of data type String, and the string contains a valid date, the Date object contains a time value based on that date.

I’m currently working on a 2D RPG Game Engine ( think Zelda or FinalFantasyII/IV ) in AS3. At the start of this project, as typical projects with friends go, it seems like a completely easy task. “Hey you know AS3… can you write us a quick game engine for this idea we have?” Sure… I’ve finished a simple little game before, I can totally do that.

Take 1:

From previous (limited) game dev experience I know I’ll need to set up a heirarchy of classes to handle Player Characters, Enemies, Scenes, Buttons, etc. So, thinking things out logically (read: noobishly (?) ) the first thing the team will want to see is going to be a the big-picture, visual framework. I sit down and start hammering out a SceneManager to handle scene changes easily. I’ve created an IZFScene interface to standardize the methods of all of my Scene objects for the SceneManager to manipulate. I’ve got my placeholder backgrounds that I tossed into the Flash Library from photoshop ( or MSPaint ) and I’m off and running creating smooth fade-in/fade-out transitions between scenes. The engine process looks like:

Read the rest of this entry »

Tags: ,

12
Mar

AS3 – AMFPHP High Score Database

   Posted by: Haelix   in Actionscript 3, PHP/MySQL

This is a little tutorial covering using ActionScript 3, PHP and AMFPHP to create a MySQL-based High Score Database.  You should have some familiarity with each as this isn’t exactly a “Beginner’s How-To.” For a recent game project I’ve been working on, one of the requirements was a simple High Score Database. After finishing it, I thought I’d post about how I went about coding it. Let’s jump right in with the ActionScript first…

So from the game’s .as files, the idea was to display a DataGrid that shows all the scores submitted to the database. I also wanted to create a ScoresDB class that handles all of my database calls and parses the database results, all ready to be added to the DataGrid.

So that we’re all on the same page, Main.as will refer to the main class that handles adding the DataGrid to the stage, and handles other game functions. ScoresDB.as will refer to the ScoresDB class that handles the AMFPHP/PHP/MySQL calls. HighScore.php will refer to the AMFPHP Service that actually interacts with the MySQL database and returns result sets.

Read the rest of this entry »

Tags: , ,