Pages

Tuesday, April 5, 2011

Unity3D: 2D game development...

2D game development
2D games has always been my weakest point in game development, I remember in 2009 when I first started using AS3, I was a total mess, I write codes that could make people laugh to death (yeah, it's that serious!). Have you ever seen a function within a function (2 dimensional function)? Something like this:



function ShootBullet()
{
  function CollideEnemy()
  {
    function Explode()
    {
    }
  }
}

It was a really embarrassing stuff to reveal to people that I once had a moment like that, especially when during that time I was a very over-proud-of-myself and told people about how good I am in scripting AS3, and when they ask me to show my codes, I was like... connection problem, I forgot about it, and make them forget it slowly. Asides from bad scripting, I can hardly code a workable game, except for some clicking and touching game that earn you points, like those lame old "tap, tap" iPhone game... Those method they use are a bit too hard for a design student like me to understand, and I slowly developed a phobia against 2D game development... 

Anyway, I've been doing 3D game development for almost a year now, in Unity3D, which was a great piece of game making machine. I've also tried XNA, UDK, but non of them grab my heart long enough like Unity3D did. Now, for my internship program, I was tasked to developed a 2D game for the iPhone, iPad, and Android phone, with mechanics a bit similar to Doodle Jump without the jumping part. So, for the past few weeks I've been busy working on the game, in an area (or dimension) which I'm still kinda "fresh" at. 

Animating 2D Sprites
I've never touched sprites all my life, not even in AS3... I mean... not like... a whole bunch of sprites in a piece of sprite sheets and then assign them one by one in correspond to a certain time, to make looks like it was animating. As far as I know, animating 2D sprites was a lot easier with SpriteManger, but that wasn't my primary choice for the project. I chose to load up a couple of 2D individual sprites and then animates them using a timer function, which, compare to SpriteManager, was a lot easier. But the downside with this method is that, it takes a LOOONG time to load up all of the textures. Imagine if I export it to iPhone, it would be a disaster length of time to load up those 400+ 2D individual sprites the artist prepared.

Now what I meant by "2D individual sprite" (it's not a correct term, I just invented it) is this: 
I have about 8 of this crabby image (which animates when you play them in Flash), instead of a piece of sprite sheet with 8 of them inside.

From what I hear earlier, creating a Texture[] container and then drag and drop these textures into the container will results in increasing the loading time of the game.
So what I did next to solve the long-loading problem was to load the images right after the game finish loaded and started, using Resources.Load():
public Texture[] crabby; //the Texture array for storing the loaded textures
public int texNo = 8; //number of textures to be loaded

void Start() {
  crabby = new Texture[texNo]; 
  
  for (int i = 0; i<texNo; i++)
  {
    //in order for the textures to load successfully, 
    //all the textures must be placed in a folder named "Resources"...
    crabby[i] = (Texture)Resources.Load("crabby_" + i, typeof(Texture));
  }
}

And that was before I realize that I could flip the texture of an image using Material.SetTextureScale(), I flipped all of the textures manually using Photoshop, and then load all of them using the method I use above and store them in two 3 dimensional arrays (the 3 dimension: level, enemy type, and textures). 

Just when I thought I finally found a solution to my long-loading problem, I realize that it was a waste of effort, because the game was only a bit less laggy than before by a bit. The problem occurs when I had to load the textures of 19 different enemy's 8 individual sprites, which has both left and right space, and adds up to a total of: 19 x 8 x 2 = 304 textures. 

It was a stupid move, I know, but during that time, the project was about close to deadline, and there's no time for me to go and learn something new, asides from trying other solution I could think of (other than SpriteManager, which was thought to be extremely hard to learn at that time). 

So the huge laggy issue finally appear and I finally took a step into the world of SpriteManager. And to be honest, it wasn't really that hard to learn at all. The original developer even have a newer version of it called SpriteManager2, which was said to be way more better and easy to use than the original one. Too bad I can't afford it at the moment, so I'm just gonna stick to the first. And if I have the time, I might even post one or two tutorials about it... and today is only the second day I'm trying out the SpriteManager though... :D

No comments: