Archive for May, 2009

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