Call: (209) 560-6611

Stage scroll with player as3

Date: January 3, 2015 Author: ang Category: Tutorials Tags: , Comments: 0

Here is a function in ActionScript 3 that allows the stage to scroll with the player. This is perfect for platform side scrolling games. Variable 'player1' is the name of the player movie clip and level1 in the name of the movieclip container containing your level content, platforms, enemies etc.

 Javascript |  copy code |? 
01
02
stage.addEventListener(Event.ENTER_FRAME,stage_x);
03
 
04
function stage_x(e:Event)//scroll stage with player
05
		{
06
			var distance:Number = player1.x-((stage.stageWidth/2)+offset);
07
			var ease:int = 5;
08
			var offset:int=10;
09
			if(distance<0){
10
				distance*=-1;
11
			}
12
 
13
			if(player1.x<(stage.stageWidth/2)){
14
				var variable:int=distance/ease;
15
				level1.x+=variable;
16
				player1.x+=variable;
17
			}
18
			if(player1.x>(stage.stageWidth/2)){
19
				var variable2:int=distance/ease;
20
				level1.x-=variable2;
21
				player1.x-=variable2;
22
			}   
23
		}
24

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you post, please prove you are sentient.

what is 9 plus 4?

Before you post, please prove you are sentient.

Earth orbits the ...