Wednesday, March 24, 2010

Back to basics! A significant increase in speed and smoothness, and some tips to optimize AS3 efficiency for games

UPDATE: Looks like the serious lag issues occur when you hovered over the character...click and hold the guy, wait for it to slow down and then move off the screen with the button still held down. I'm getting closer to solving this...

So after about two weeks of basically restarting this whole process, and optimizing and making my code more efficient, I came to realize that this was probably a dumb idea.

What I should have done in reality is worked on adding to the game, but I became increasingly annoyed when I had about 30 enemies and yo ucould noticeable tell there was a significant lag. And the worst is, its still not complete, I'm having trouble with Mouse Listeners, specifically when you control and hold a main character.

Here's a look at the game so far, and you can see that it lags significantly when you just click and hold a red guy, but then when you release, the game smooths out again:

So you can see that it works great, and its because of a few things I smoothed out in my code:

  • I removed almost ALL timers. I had timers for any particular event, for walking, waiting, throwing, loading, getting hit (actually this one still remains), the animation for when a snowball hits the floor, you name it, it was most likely timed. But I did remove them and stop them appropriately, but I have a feeling the "new" keyword was really killing my efficiency...which leads me to....
  • Creating too many new objects almost every frame. I don't know why I ignored this, but I had new snowballs being created for every throw, this was a big no-no; I even had a new object being created everytime for when you hovered. Not to mention the footprints about every 20 pixels. So a new footprint X 54 guys X 54 new timers being created (since I faded them out after about 3 seconds, then destroyed them) really puts a damper on your game smoothness.
  • Too many checks of unnecessary collisions. I definitely had some objects being checked twice, ie: I had a giant array for all snowballs, and I had to do a check each time so that a green snowball wouldn't react to another green snowball. Brutal. I ended up separating the arrays and really doing a check with green snowballs vs. 3 red guys.
  • Overuse of Math functions. I'm talking about the Math (dot) notation. I used it almost every throw for Math.sin, Math.PI, the worst was that every guy had their own independant listener, and that EACH frame would do a Math.abs to determine if they reached a target point. Check this site to optimize some basic math notation! Also I replaced Math.PI with a global variable set to 3.14159.
  • Vectors instead of Arrays. I haven't actually implemented this yet, since I'm a little lazy, but I will try it out. I hear this really helps out. And avoid using array.push and DON'T USE SPLICE OR DELETE!!! Try to reuse these objects, but if not, I would suggest setting removing them from the DisplayList (via removeChild(object:MovieClip)) and nulling the array index. Then always avoid checking null indices.
  • Reuse objects you already have on the screen. I now only create brand new snowballs if all current snowballs are either in the air, or none have been created yet. So basically as soon as a snowball hits the ground, I set that alpha to 0 and save it for later.
  • Use Sprites. I haven't done this either, but creating Sprite sets (or Tile Sheets) and using pure Sprites (which are one-frame movieclips with basically all the event functions) really can help for efficiency.

I have to still update the other levels, currently I'm really stuck on Mouse Listeners. It just seems that when you click down on a character, and hes ready to throw, the gameplay eventually gets really, r e a l l y, r   e   a    l   l    y, SLLLLLOoOoOOOW. But then let go and see the gameplay resume to normal.

Posted via web from VG Games

Thursday, March 11, 2010

This isn't so much an update, but more like a...downdate.

I went a little overboard with AS3 Timers. I was working on more level 6 fixes when I came across a massive lag issue, turns out when I have about 30 guys, it starts to slow down (try it, have them follow you around and start moving your red guy a lot) and the smoothness is gone. I wouldn't complain so much, because 30 guys is ridiculous in itself, but I want to obviously add a lot more than just the basic game formula, so it will require more memory. SO I have to go back and see if I can improve it.

As a developer-in-training I've noticed some improvements when I switch some time-based to frame-based animation. So I'm going to convert this to purely frame-based. I was reading this article by Keith Peters, talks about Timers and their efficiency. You should check out his book if you want to get into AS3.

Click here to see the original post for embed options!

Posted via web from VG Games

Monday, March 8, 2010

A note about this blog

I will maintain this blog, thanks to Posterous' Autoposting technique, but there's very little I can do to the theme of this site. Look how brutal it is, I hope I've salvaged something with the header image, and of course, the high quality flash gaming content I'll be posting from time to time.

Alright enjoy the game, remember this is your game too, so you have an idea, send it in!

Yes, even MORE Snowfare level 6 updates and fixes - and 20 enemies to fend off!

Still going with the changes, I found so many errors with organization. I think I’ve managed to work it out, and I have it running pretty smoothly, I included 20 enemies for you to attack, and it doesn’t seem laggy.

COMING UP: Level 7 in progress, have guys completely resurrect, as well as dodge whilst on the floor. I think I’m going to start distribution a lot better, as in not just attack the closest guy. This isn't even the tough part yet...Snowforts, items, and bosses are going to be ridiculous!

Click here to grab the embed code!

Posted via web from VG Games