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.
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 | |