Link to home
Start Free TrialLog in
Avatar of srbentley65
srbentley65

asked on

Why is Actionscript vertical scrolling so choppy?

Having just had a failed attempt at getting a scrolling game working in HTML5, I went to Flash, the gold standard of web games expecting to see an amazing improvement in my simple vertical scrolling game.  But alas, I must be doing something dreadfully wrong.  

I built the simplest example I could just to see an endless stream of obstacles move top down for the player to avoid, but the scrolling is very choppy. Flash must be better than this so it has to be something fundament I'm doing wrong.  I experimented with a variety of FPS (what is recommended?) and adjusting the value of the Y position on each step.  

I have attached the project files (except the FLA which is forbidden apparently) for reference as it could be a setting or my approach of using addChild and then adjusting the Y value during the ENTER_FRAME event.  Any guidelines that could improve this scrolling would be greatly appreciated.
ScrollTest.zip
Avatar of dgofman
dgofman
Flag of United States of America image

Please save your FLA file as CS4 File->Save AS (in dropdown file type select) "Flash CS4 Document" than you will able to attach another way archive file using RAR format
Avatar of srbentley65
srbentley65

ASKER

I'm using CS6 and didn't see that option. I am posting a zip externally on Zip with FLA file. Hopefully that will suffice.
I can see rocks are and can move a boy what do you want to do now?
ASKER CERTIFIED SOLUTION
Avatar of dgofman
dgofman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't necessarily want to rely on FPS because I thought that makes a lot of assumptions about the target device it's running on. So I'm open to the best approach to smooth scrolling animation.  Your solution does look much better than what I started with.
When I compile that and post it online, this is the result:
Scroll Test Flash Result

Does that scrolling acceptable?  It still looks jumpy to me.
You annimation is very smooth what you don't like it?
You can also try moving canvas of the rocks

package
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class GameController extends MovieClip
	{
		private var player:Player;
		private var moveX, moveY:Number;
		private var myTimer:Timer;
		private var rockMode: Number;

		private var _enemies:Object;
		private var _rockCanvas:MovieClip;
		private var _speed:Number;
		private var _y:Number;

		public function GameController()
		{
			_y = 0;
			_speed = 4;
			_enemies = {};
			_rockCanvas = new MovieClip();
			addChild(_rockCanvas);
		}
		
		public function timerFunction()
		{
			var newRock = new Rock();
			
			// just for testing...
			switch (rockMode)
			{
				case 1:
					newRock.x = 30;
					rockMode++;
					break;
				case 2:
					newRock.x = 120;
					rockMode++;
					break;			
				case 3:
					newRock.x = 50;
					rockMode = 1;
					break;
			}
			newRock.y = (0 - _y - newRock.height);
			_rockCanvas.addChildAt(newRock, 0);
			_enemies[newRock.name] = newRock;
		}
		
		public function gameStart()
		{
			timerFunction();
			setInterval(timerFunction, 1500);
			
			rockMode = 1;
			
			//enemies = new Array();
			
			player = new Player();
			player.x = 400;
			player.y = 450;
			mcGameStage.addChild(player);
			
			//playerScore = C.PLAYER_START_SCORE;
			
			mcGameStage.addEventListener(Event.ENTER_FRAME,update);
			
			//Handle event when this game is being preloaded
			addEventListener(Event.ADDED_TO_STAGE, gameAddedToStage ); 
			
			//Handle situations when this game is being run directly
			if (stage != null)
			{
				stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
				stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
			}
		}		
		
		private function gameAddedToStage(evt: Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
		}		
		
		private function keyDownHandler(evt:KeyboardEvent):void
		{
			if ((evt.keyCode == 37) || (evt.keyCode == 65))
			{
				//Move player left
				moveX = -1;
			}
			else if ((evt.keyCode == 39) || (evt.keyCode == 68))
			{
				//Move player right
				moveX = 1;
			}

		}
	
		private function keyUpHandler(evt:KeyboardEvent):void
		{
			if ((evt.keyCode == 37) || (evt.keyCode == 65))
			{
				moveX = 0;
			}
			else if ((evt.keyCode == 39) || (evt.keyCode == 68))
			{
				moveX = 0;
			}
			else if ((evt.keyCode == 38) || (evt.keyCode == 87)) 
			{
				//moveY = 0;
			}
			else if ((evt.keyCode == 40) || (evt.keyCode == 83))
			{
				//moveY = 0;
			}
		}
		
		private function update(event:Event)
		{
			//******************			
			//Handle User Input
			//******************

			//Handle Player 1 User Input
			if (moveX > 0)
			{
				if (player.x <= 700)
					player.x += 6;
			}
			else if (moveX < 0)
			{
				if (player.x > 50)
					player.x -= 6;	
			}

			_y += _speed;
			_rockCanvas.y = _y;

			//Update enemies
			for(var name:String in _enemies)
			{
				var rock:Rock = _enemies[name];
				if(_y - Math.abs(rock.y) > stage.stageHeight) {
					_rockCanvas.removeChild(rock);
					delete _enemies[name];
				}
			}
		}
	}
}

Open in new window


package  {
	import flash.display.MovieClip;

	public class Rock extends MovieClip {

	}
}

Open in new window


And you will get better performance if you will initialize first your rocks before start moving this canvas.