Klok

September 23rd, 2008 - No Responses

Keeping track of time is always a bitch on a given project. Of course you could always just keep a notebook handy (which I have done a lot) and jot down hours, but its so much better to have a program do it for you. Klok is just that. Its a simple, clean, straight up app available here. It’s been pretty cool for me so far in the week or two that I have been using it and I recommend it for a FREE time tracking tool.

Game Object Base Class

April 13th, 2008 - No Responses

In a previous post I talked about having world objects be described in numbers and bitmap data. Then if this object (in memory) is within a viewable rectangle render it to the screen. The code of this post is a simple example of such a class. Also, when building such a class for your game make sure you adhere to an interface.

Check out the example

Game Object Management

April 13th, 2008 - No Responses

Having a simple way to keep track of world objects in a game is paramount. I have a simple system that I often use in my games that I presented on at MN.swf camp this last week. It’s by no means a standard and it can be added on to at great lengths. I just dig it because its simple and easy to use.

The idea behind this example is to use the Dictionary object with an a generic object. Reason being is that we can then use the actual game objects as keys and values. Then if we set the weak reference to true we can get rid of this world object all the easier. Also, you will notice that I used a try catch in the GameObjectManager. Reason being is that I found that when I removed an object, within that same frame I would be iterating over the collection and garage collection had not yet deleted that reference. I don’t know if I am doing something wrong or if this is a bug with the Flash player; if someone has an answer please leave a comment or drop me an email.

Check out the example

Blitting within a Rectangle

April 12th, 2008 - No Responses

Often in game programming you will have a large world but you only want to render a certain viewable area of it. By doing a simple check of where an object is in the world you can render only the objects wanted to the screen. This will help out performance dramatically. The next idea behind this is to cut down game objects only to numbers and bitmap data, instead of display objects.

Check out the example

Blitting in Flash

April 12th, 2008 - No Responses

Next is simple example of blitting to a bitmap in Flash. The idea is to have one bitmap used for the screen and lock and unlock it every frame and draw any objects wanted to the bitmap. But don’t forget to fill the bitmap first else you will have pixels leftover from the last frame resulting in an unwanted ghosting effect.

Check out the example
**Note: Thanks to Electrotank in this whole series for the FPS counter.

Vector Quailty vs. Bitmap Data Quality

April 10th, 2008 - No Responses

I have always been curious about this and was happy to cook this up for my talk at MN.swf camp this past week. The example is simple; I am caching bitmap data into an array (like the last example) and putting it side by side to a vector animation. Sure you can tell a little bit of a difference but it isn’t significant. Especially when the example is scaled down (like it should be) then it doesn’t matter at all. Pretty freakin’ sweet!

Check out the example

Converting Vector to Bitmap Data

April 10th, 2008 - No Responses

In this the second post in Flash Player rendering techniques I am going to look at converting vector animations to bitmap data on the fly. This is not a hard and fast way of pulling this off; it’s just a way that I have done it in the past. Feel free to take it, run with it and make it better.

Check out the example

Flash Vector Animation Benchmarks

April 10th, 2008 - No Responses

Recently I was invited to speak at MN.swf camp during which I spoke about Vectors, Bitmap Data and Game Object Management. It was a great experience and I thank everyone involved for the opportunity. Since I have all these files sitting around and I haven’t posted in about 3 months it might be a good idea to share this with everyone.

This will be the first in a series of many posts on Flash rendering benchmarks and techniques. I have many people to thank for this my peeps over at 8bitrocket, Jessie Warden and Jobe Makar.

This example shows how the Flash player will dog with a complex vector timeline after about 100 objects onscreen with minimal code. Check out the example

Event Bubbling AS3

January 17th, 2008 - One Response

Getting used to new features (and discovering them) in AS3 will take some time. My friends over at 8BitRocket realize this just as much as I do.

Something I heard about over and over again was event bubbling. I knew what it did; I knew it would be a good thing to use in my programs… But… I never used it. So I whipped up a quick test tonight after thinking about a common “code smell” problem I have been finding myself doing. In the game in question I constantly pass in references of parent classes into children (don’t laugh). In a true event driven program event bubbling takes care of this. Why pass in a reference when a child should dispatch an event when there is a change and naturally the parent should hear it. Makes sense.

Here would be a typical parent-child relationship in a game of mine: Main –> World –> Player –> Ship.

When a ship would get hit, die or shoot, the parents should know about it via event bubbling not references. The parents would then listen to events that they would care about and just not to ones that are irrelevant.

The code demos a ship making a circle clickable and the world hearing it. It also dispatches an event at 100ms and the world hears that as well. Remember in your event dispatch to have event bubbling set to true. Example: dispatchEvent(new Event(”shipEvent”, true));

Here is the code for the four classes: (sorry for the shitty code formatting, copy and paste into an IDE)

Main:
package
{
public class Main extends Sprite
{
private var _world:World;
public function Main()
{
_world = new World();
addChild(_world);
}
}
}

World:

package com.pacyga.world {
import flash.display.Sprite;
import com.pacyga.player.Player;
import flash.events.MouseEvent;
import flash.events.Event;
public class World extends Sprite {
private var _player:Player;
public function World() {
_player = new Player();
addChild(_player);

this.addEventListener(MouseEvent.CLICK, clickHandler);
this.addEventListener("shipEvent", shipEventHandler);
}
private function clickHandler(evt:MouseEvent):void {
trace("[World].clickHandler()");
}

private function shipEventHandler(evt:Event):void {
trace("[World].shipEventHandler()");
}
}
}

Player:

package com.pacyga.player {
import flash.display.Sprite;
import com.pacyga.gameobjects.ship.PlayerShip;
public class Player extends Sprite {
private var _playerShip:PlayerShip;
public function Player() {
_playerShip = new PlayerShip();
addChild(_playerShip);
}
}
}

Ship:

package com.pacyga.gameobjects.ship {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class PlayerShip extends Sprite {
private var _shape:Shape;
public function PlayerShip() {
_shape = new Shape();
_shape.graphics.beginFill(0xFF8800);
_shape.graphics.drawCircle(100, 100, 50);
this.addChild(_shape);
var timer:Timer = new Timer(100, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
timer.start();
this.addEventListener(MouseEvent.CLICK, clickHandler);
}

private function clickHandler(evt:MouseEvent):void {
trace("[PlayerShip].clickHandler()");
}

private function timerComplete(evt:TimerEvent):void {
trace("[PlayerShip].timerComplete()");
this.dispatchEvent(new Event("shipEvent", true));
}
}
}

Singleton Enforcer AS3

January 17th, 2008 - No Responses

Just a helpful way to do Singleton’s in AS3 since there is no longer private constructors:

package
{
   public class SingletonClass
   {
      private static var INSTANCE : SingletonClass
      public function SingletonClass(enforcer:SingletonEnforcer)
      { // construction }
      public static function getInstance(): SingletonClass
      {
         if(INSTANCE == null)
         {
            INSTANCE = new SingletonClass(new SingletonEnforcer());
         }
         return INSTANCE;
      }
   }
}
class SingletonEnforcer{}