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: , ,

· · · ◊ ◊ ◊ · · ·

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: , , ,

· · · ◊ ◊ ◊ · · ·