The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: First real game project - Performance Tips?

  1. #1

    First real game project - Performance Tips?

    Hi Guys.

    I've tried making a game for my friends website. They've gone ahead and integrated it with their system but I still worry that performance is not that great, frame rates are slow on older computers and route finding is incredibly slow especially when the user has made a maze and changes a route.

    Thats a link to the free game on the site if anyone gets a chance to let me know their thoughts or ideas on how to make it all a bit faster?

    The way I've tried to do the path finding is using A*, and every time I find a route from a tile I save that route information in a tile array. So next time a creep needs a route from that tile, the route doesn't need to be regenerated, it can just be copied over to the creeps local array. Also when a new route is generated, every tile in the path will be updated with the new route.


    Tile A gets new route:
    Tile A Route = {Tile B, Tile C, Tile D - Finish Tile)
    New route gets copied to other tiles on the route
    Tile B Route = {Tile C, Tile D - Finish Tile)
    Tile C Route = {Tile D, Finish Tile)

    But even with me trying to be clever like this route finding is very slow, the only extra idea I have is to make route finding find the quickest path to a known valid route, instead of always finding the route to the finish point. Hopefully there will be gains from this, but any other ideas would be great!

    Thanks in advance guys!

  2. #2
    How often/under what circumstances do you recalculate routes?

  3. #3
    When a new tower is placed all tiles will be checked as 'checkRoute=true'. Then next time a creep requests a route from that tile, it will check to see if the route is still valid. If it's not valid, only then will the A* algorithm be called to ask for another route. Also when a tower is sold, a new route is generated regardless of whether the existing route is valid or not (maybe theres a quicker route now a tower got sold?)

    Couldnt think of a better way of doing that part of the route finding stuff.

  4. #4
    Could you spread out the route calculations over several frames? Let's say a new tower has been placed somewhere that blocks all best routes. Rather than recalculate everbody's route one the next frame, just recalculate the route for one unit. The next frame, recalcuate the route for the next unit. This would spread the load of route calculation over multiple frames.

  5. #5
    When a new creep group gets released, they 'sleep' for a random amount of time (the range of the random value is determined on the type of creep, some come out in tightly packed groups so have a small random range). Anyway they wont look at the route for the tile they are started in until they are 'woken' from their sleep. This way the A* calculations are spread over many different frames.

    I think that the biggest problem for me is that frame rates are slow on older computers when users have built a bit of a maze and each tower is targetting a creep and firing projectiles at them. I've tried to combat this in a lot of ways including: getting rid of gradients where possible, removing maths functions and replacing them with lookup tables (SIN/COSINE) and constants and a few other things. If anyone has any clever ideas about how else to optimise flash movies like this, I'd be very happy to hear it

  6. #6
    ok so im not sure how to optimize your code without seeing it for one and another just a little quirk in the game, you should probobly put a marker of some sort as to where the enemies are gonna come out since when i started the game it was kinda like okay 4 doors 50-50 chance of getting it right.

  7. #7
    I've been working all night on trying to cut down the amount of time the A* routine takes and have managed to get it from 600ms in the worst case scenario to 50ms in the worst case....

    Is there much need for A* flash tutorials / code examples for tile based games? If there is I'll try publishing some of the code for people to use / improve as they wish.

    I think most of the problems now are with the time it takes to render graphics, especially later on in the game. If anyone has any tips or tricks to speed up render time then that'd be great.

    Just incase I'm doing something really dumb in my main loop... here it is:

    function doCreeps()
            aStarDoneThisFrame = false;
            for(var i10=0;i10<_root.liveCreeps.length;i10++)
                    _root.liveCreeps[i10].doMovement(); else
            for(var i10=0;i10<=_root.liveProjectiles.length;i10++)
                if(_root.liveProjectiles[i10]._visible ==false)
            if(fps%2) // Towers need to be called half as much as creeps+projectiles
                for(var i9=0;i9<=_root.towerList.length;i9++)
                    if(_root.towerList[i9].towerOnDisplay._visible ==false)
                        { // Tower is sold, clean the tile.
                        towerList[i9].rangeImage._visible = false;
                        trace("unloading tower towerlist now "+towerList.length);
    var manageCreeps = setInterval(doCreeps, 1000/30);
    Thanks guys

  8. #8
    haha your game is fun and addictive but so crazy hard. i didnt find it slow on my computer and mines not that great. good job man

  9. #9

    The guy I did the game for has now told me he needs Towers to be offset against each other which makes route finding far more difficult... I've got this far so far:

    (sorry for the huge image)

    This has meant A* is now not fast enough again so back to the drawing board! Back to the drawing board I guess Still open to any advice on speeding rendering info.

  10. #10
    Registered User
    If your really going back to the drawing board do the game in AS3, its fast enough to use A* directly. The AVM1 does have its limitations and speed wise palls in comparison to AVM2.
    Also its not nearly as optimized as it could be.
    For instance slower
    for(var i10=0;i10<_root.liveCreeps.length;i10++) {
    var length = _root.liveCreeps.length
    for(var i10=0;i10<length;i10++) {
    Last edited by Gathan; June 1st, 2008 at 03:04 PM.

  11. #11
    Wow thanks, I use those kind of For loops everywhere so hopefully that will help out a bit. Also I wasn't aware AS3 was much quicker than AS2, I'll try searching for some AS3 tutorials and try to find out what the key differences are. Hoping its syntactically quite similar though, this games ended up being ~5,000 lines long!

  12. #12
    One hot tip for speeding up A* is using priority queues it can probably speed it up 3-4 times.

  13. #13

    I don't see how adding a superfluous variable will speed up your code, especially if there is only one loop.

  14. #14
    Quote Originally Posted by Insane au View Post

    I don't see how adding a superfluous variable will speed up your code, especially if there is only one loop.
    Array.length calls the array's getter method, which will be called once per loop. Presetting its value outside the loop will avoid this call. Someone has tested this and confirmed the benefit for AS3:

  15. #15
    Sorry i hadnt checked back here in a few days, thanks for your messages.

    I hadnt heard of priority queues before with A*, I'll try to find some examples on the web, unless anybody has some good articles bookmarked?

    The rewrite in AS3 is almost complete now and can be viewed at:

    It seems a lot quicker on my computer, although computers with no graphics cards / graphics cards not compatible with flash really struggle with the rendering which is a bit of a worry.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012