0

I've been programming a side scroller based on a tutorial found in a book. My friend did the same, and his is working perfectly.

I've only really changed a few variable names around (I've also done animations differently) but for some reason, when my character is moving, there is a large amount of lag.

HOwever the lag is only present when there are 'walls' on the stage. When I scroll past them, the lag goes away, then returns.

Walls and Floors both use the same code (they are both assigned as 'floorObjects' variables) and use the same collision code, however I cannot figure out why there is lag involved.

From where the character starts (about 60x) if the character goes left, there is a HUGE amount of lag. If I go right, there isn't too much lag, until the screen starts to scroll.

The lag from going left I believe may have something to do with the program being preventing from scrolling off the map etc. But I can't figure out why there is lag when trying to move right.

I've listed the Scroll code, and the main loop, if need be I can upload the collisions code, and any help would be greatly appreciated.

Scroll code;

public function scrollGame()
        {

            var stagePosition:Number = gameLevel.x + player.mc.x;
            var rightEdge:Number = stage.stageWidth - edgeDistance;
            var leftEdge:Number = edgeDistance;
            //Scroll the GameLevel to the Left if player moves right
            if(stagePosition > rightEdge)
            {
                gameLevel.x -= (stagePosition - rightEdge);
                //Prevent the game scrolling off the stage
                if (gameLevel.x < -(gameLevel.width-stage.stageWidth)) 
                    gameLevel.x = -(gameLevel.width-stage.stageWidth);                      
            }
            //Scroll the GameLevel to the right if player moves left
            if(stagePosition < leftEdge)
            {
                gameLevel.x += (leftEdge - stagePosition);
                //Prevent the game scrolling off the stage
                if(gameLevel.x > 0) 
                    gameLevel.x = 0;                
            }

        }

Main Loop:

public function gameLoop(e:Event)
        {
            //Get Time Difference
            if(lastTime == 0) lastTime = getTimer();
            var timeDiff:int = getTimer() - lastTime;
            lastTime += timeDiff;

            //Game Cycle tasks
            //Only perform if in play mode
            if(gameMode == "play")
            {
                moveCharacter(player, timeDiff);
                moveEnemies(timeDiff);
                checkCollisions();
                scrollGame();

            }
        }

UPDATE:

So I "profiled" it, most of the time is being spent either in the MoveCharacter() function, using the gotoAndStop() command. So I removed that, and it made no difference, still lagging. I then removed the enemies also, still lagging. But turning quality down to low has somehow fixed it (though at a poor quality now ) Any ideas as to what is causing the lag and how to fix it?

NeoKuro
  • 49
  • 1
  • 7
  • Your `scrollGame` function doesn't look particularly intensive. Your `gameLoop` function calls some other methods which may or may not be slow - we can't tell. It could even just be slow at drawing the world. I would use a profiler to work out where the time is being spent, then we can advise on how to improve that particular part. – AndySavage Apr 28 '14 at 16:19
  • Thanks, I'm fairly new to Flash haha. Whats a profiler, I can't say I've heard of it before :/ – NeoKuro Apr 28 '14 at 16:47
  • AH nevermind, seems simple enough (I'm assuming Adobe Scout will suffice? :L ) – NeoKuro Apr 28 '14 at 17:10
  • So I "profiled" it, most of the time is being spent either in the MoveCharacter() function, using the gotoAndStop() command. So I removed that, and it made no difference, still lagging. I then removed the enemies also, still lagging. But turning quality down to low has somehow fixed it (though at a poor quality now ) Any ideas as to what is causing the lag and how to fix it? – NeoKuro Apr 28 '14 at 18:25
  • This sounds to me very much like it's spending time drawing rather than in AS code. A quick way to test this would be to hide content (such as your background, or anything else that is scrolling) and checking the performance. If drawing speed is your bottleneck then you should probably open that as a new question (or make this one specific) as that's a whole different avenue of optimisation. – AndySavage Apr 28 '14 at 19:39
  • 1
    Ahh. Yeah what I had done (which is what the book done) was go with a tiled system. So many 40x40 blocks, and the code had to look at each of them and check for collisions (twice per character). I ended up just merging them into large blocks and it seems to have cut down on the lag for now :) – NeoKuro Apr 29 '14 at 02:23

1 Answers1

1

this is from the flash university book isn't it?

The code is fine.

I know what will lag your flash game.

This is a guess though, and I do get this error some times.

Make sure your images are optimized!

If they're imported from photo shop or illustratro then flash will have to deal with those complicate vector points.

Use .png for transparent images, bitmaps don't hurt either.

Moynul
  • 635
  • 1
  • 8
  • 30
  • Thanks and yeah it is. I've been using Photoshop, and saving as .png for all my images (transparent or otherwise) but it imports as bitmaps. I then have nearly always converted to a symbol.) Do symbols increase the load, or make no difference? :/ – NeoKuro Apr 30 '14 at 01:59
  • @NeoKUro if you're going to use an image two times, convert it into a movie clip, it takes up less memory. – Moynul Apr 30 '14 at 02:03
  • Hm ok, theres only one pair that really do that, thats my clouds (that loop) but I intend to make it parrallax scrolling, so only need 1 in the end, but right now the game is too laggy. I think it may be the drawing, but my friend has many more objects than I do, and his runs fine, he said he just "optimized his code" by putting stuff into functions as much as possible instead of repeating it. But not sure if that'd make much of a difference with mine. – NeoKuro Apr 30 '14 at 03:07
  • how big is your swf file? along with your .fla file? – Moynul Apr 30 '14 at 03:07