If you’ve got flash communicating with a server, using UNIX time can be more robust then strings. UNIX time is often used in SQL databases and on servers. Using an int for all numbers means you don’t have to deal with daylight savings and other events.

To convert a UNIX time into an ActionScript Date object:

/**
* converts UNIX time to flash Date()
* @param time number - unix time to convert
* @return Date object
*/
public function getDate(time:Number):Date {
	var mtime:Date = new Date(1970, 0, 1, 0, 0, 0);
	// Add on the number of seconds (this is our unix timestamp)
	mtime.setSeconds(time);
	return mtime;
}

After you have a data object that was set by a unix stamp, you can treat it like a date object.

Sadly Adobe’s toString() function makes the date object ugly and unreadable. Date.toString() returns something like: Fri Apr 15 02:44:33 GMT+1200 2011. This is:

  • horrible to look at
  • requires users to lookup GMT to get their local time
  • will put the users off reading nearby text

It may seem trivial to make it readable, but you might as well not have it if it’s not being read. The fix is simple: take the date and work out how long ago it happened.

To make a UNIX time more readable:

/**
* converts a UNIX time to a human readable time scale (eg 20 mins ago)
* @param time time in seconds since 1970, 0, 1 (UNIX time)
* @returns a string with minutes, hours or days ago event happened
*/
public function human_time( time:uint ) : String {
	var now:Date = new Date();
	var diff = now.time / 1000 - time;

	if (diff <= 3600) {
		var mins:Number = Math.round(diff / 60);
		if (mins <= 1) {
			return "1 min";
		}
		return mins +" mins";
	} else if ((diff <= 86400) && (diff > 3600)) {
		var hours:Number = Math.round(diff / 3600);
		if (hours <= 1) {
			return "1 hour";
		}
		return hours+" hours";
	} else if (diff >= 86400) {
		var days:Number = Math.round(diff / 86400);
		if (days <= 1) {
			return "1 day";
		}
		return days + " days";
	}
	return "unknown";
}

This will return something like 3 hours, 20 days or 2 mins. The user will more likely read it and know how long ago it happened.

Some notes: the local computer must have the accurate time and it doesn’t accomodate times in the past. Modern computers sync using the net this shouldn’t be a problem and a simple mod can accommodate times in the past

Tags: , ,

· · · ◊ ◊ ◊ · · ·

Anyone who uses AMF, dpdk.nl’s ActionScript 3.0 remoting library is a fantastic robust, remoting service.

There are “rare” events that I didn’t consider after making several projects with it. These events, such as a connection issue, results in the user watching a spinning wheel for 10 minutes not realizing the app has jammed.

Inside nl.dpdk.services.remoting.RemotingEvent you’ll see four ‘enums’:

  • ERROR_NETSTATUS
  • ERROR_IO
  • ERROR_ASYNC
  • ERROR_SECURITY
  • ERROR_TIMEOUT

You’ll want to take care of each event, but ERROR_NETSTATUS and ERROR_TIMEOUT should be suffice. Here’s how you deal with ERROR_NETSTATUS:

var service:RemotingProxy = new RemotingProxy('http://localhost/', 'domain.MyService', ObjectEncoding.AMF3);
service.addEventListener(RemotingEvent.ERROR_NETSTATUS, onNetError);
public function onNetError(event: RemotingEvent) : void {
	errorCallBack('Connection Error! ' + event.getMessage());
}

This way you can deal with connection errors, etc. A user would likely close your site and never come back if it’s freezing all the time. Giving the user the option of resending the data would keep them there, and perhaps jiggle their cat 5′s.

Tags: ,

· · · ◊ ◊ ◊ · · ·

Bad programmer. You don’t deserve the cookie.

When developers approach a problem, we smash it into bits and work on the hardest, jagged issues first. We get so focused on solving the challenging issues, we forget about the rest. It’s no surprise, as we do it for the challenge not the reward. But the fix is more complicated than a good attention span.

First, an example: I wrote a bezier curve algorithm that used bitmap blitting to make drawing on screen very smooth & responsive in flash. It was so awesome, I made it into sketchswap clone. The artists soon wanted an eraser tool and colored lines.

Erasing lines seemed very simple, just detect lines near the cursor and remove them. Circle-line collision seemed something developers would come up with:

It took a day to write the collision math, then the code, then the undo stack. But it didn’t work! sometimes this solution would remove lines on the other side of the canvas. This ghost removal was caused by an over flow of a Number value, fixable by applying CS skills to divide it in smart places. I soon found another issue: if the curve was long, the two new lines would warp. This is because curve is determined by previous lines.

Every time I fixed it, another issue would appear: If the line was far from the bezier it would break. If the user erased to fast it wouldn’t detect in-between points. This math solution just won’t work. Soon fixes got too hard: change the bezier to lines? bitmap the vector image then rewrite the backend?

It was now 1am and I decided to go to bed. As soon as my head touched the pillow I came up with a simple and perfect solution: one that wouldn’t break the undo stack, let the drawing remain intact and behaves as expected.

Just make a thick line the same color as the background.

To summarize:

  • Take a step back and ask why you’re doing this
  • Take a break one every 50 minutes
  • Don’t ask how, ask why

You’re probably working in a hole you’ve dug right now. Having fun, but getting messy.

Tags:

· · · ◊ ◊ ◊ · · ·

Flixel 2.0 vs Love2d

02 Mar 2011

Choose Flixel over Love2d.

I recently made a post about Pygame vs Love2d. I declared love2d a winner by a whisker (unless the project you are working on is huge). That post didn’t mention details, eg: setup time, language and art resources.


Flash builder environment, it’s based on Eclipse so it’s good.

Why Flixel is awesome:

  • Making a game takes 5 minutes. Really.
  • You can run it on anything (ex iPhones)
  • It looks pro, (choosing pixel art = fast dev and looks cool)
  • ActionScript 3.0 rocks, it’s a better version of Java
  • You spend more time thinking about your game, instead of how it works
  • Graphics things (eg blend modes) to make your game pretty

If you’re participating in a game jam, expect to get 2,000 hits on a flash page instead of 30, people hate downloading .exes. If you want to $ell your game, you can can make an .exe or .app to distribute and no one will know you cheaped out.


Love2d working environment. Scripts and tabbing, yuck!

Why Love2d sucks:

  • Lua is not a game dev language (eg: lacks basic ADTs)
  • Lua’s documentation/API is crap
  • Love2d lacks optimizations, (eg: no sprite group, so bliting 1,000 objects is slow)
  • The Garbage Collector is shit, it will delete at random times causing choppy behavior & crashes
  • Distributing takes ages (expect to waste 1 hr for both mac & PC)
  • No good examples
  • Most IDEs don’t support lua (setting up an environment is nasty)

I admire both their licences: Love2d = zlib, Flixel = “do what ever you want”. Flash is of course proprietary and is needed to run Flixel. So you may have to display a Adobe Flash logo if your game becomes the next Minecraft. These details are pedantic, but something to keep in mind.

So try Flixel 2.0 today. It’s awesome.

Tags: , , , ,

· · · ◊ ◊ ◊ · · ·

Flixel makes it easy to put in a loading frame into your game.

package
{
	import org.flixel.*; //Allows you to refer to flixel objects in your code
	[SWF(width="640", height="480", backgroundColor="#000000")] //Set the size and color of the Flash file
	[Frame(factoryClass="Preloader")]

	public class MyGame extends FlxGame
	{
		public function MyGame()
		{
			super(640/2,480/2,MenuState,2);
		}
	}
}

Change MenuState to your first state. Then create a new ActionScript Class called Preloader.as

package
{
	import org.flixel.FlxPreloader;

	public class Preloader extends FlxPreloader
	{
		public function Preloader()
		{
			className = "MyGame";
			super();
			minDisplayTime = 5;
		}
	}
}

You will only see the loaded if you make a release build. The minDisplayTime makes sure you can see it when loading it locally.

Of course, change all instance of “MyGame” to the class you used for your game.

Tags: , , ,

· · · ◊ ◊ ◊ · · ·