Solved

Why is Actionscript vertical scrolling so choppy?

Posted on 2013-05-11
8
521 Views
Last Modified: 2013-05-13
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
0
Comment
Question by:srbentley65
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 39158821
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
0
 

Author Comment

by:srbentley65
ID: 39158949
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.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 39158992
I can see rocks are and can move a boy what do you want to do now?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 39159013
If you want rely on FPS I am using maximum 120 but you have to change your Rock class

package  {
	
	import flash.display.Stage;
	import flash.display.MovieClip;
	import flash.events.Event;

	public class Rock extends MovieClip {

		private var speed:Number;

		public function Rock() {
			// constructor code
			speed = 4;
			addEventListener(Event.ENTER_FRAME, update);
		}

		private function update(event:Event)
		{
   		   this.y += speed;
		   if (this.y > stage.stageHeight) {
				removeEventListener(Event.ENTER_FRAME, update);
				parent.removeChild(this);
			}
		}
	}
}

Open in new window


In this case you don't need Array of "enemies"

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 enemies:Array;
		private var myTimer:Timer;
		private var rockMode: Number;
		
		public function GameController()
		{
		}
		
		public function timerFunction(event:TimerEvent)
		{
			var newRock = new Rock();
			
			// just for testing...
			switch (rockMode)
			{
				case 1:
					newRock.x = 30;
					newRock.y = -300;
					rockMode++;
					break;
				case 2:
					newRock.x = 120;
					newRock.y = -300;
					rockMode++;
					break;			
				case 3:
					newRock.x = 50;
					newRock.y = -300;
					rockMode = 1;
					break;
			}
			//enemies.push(newRock);
			mcGameStage.addChildAt(newRock, 1);
		}
		
		public function gameStart()
		{

			var myTimer:Timer = new Timer(1500);
			myTimer.addEventListener(TimerEvent.TIMER, timerFunction);
			myTimer.start();
			
			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(evt: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;	
			}

			//Update enemies
			/*for (var i=enemies.length - 1; i >= 0; i--)
			{
				enemies[i].update();
			}*/
		}
	}
}

Open in new window

0
 

Author Comment

by:srbentley65
ID: 39159856
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.
0
 

Author Comment

by:srbentley65
ID: 39161268
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.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 39161855
You annimation is very smooth what you don't like it?
0
 
LVL 29

Expert Comment

by:dgofman
ID: 39162064
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.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question