In his script, Emanuele sets up a great system for top-down character movement and wall hittesting, but if I try to change the script so that the character stays still and the background moves (for bigger levels), I get a weird glitch which moves the whole environment.
http://www.emanueleferonato.com/2010/06/07/create-a-survival-horror-game-in-flash-as3-version/
My alteration:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class survival extends Sprite
{
public var ground:ground_mc = new ground_mc();
public var environment:environment_mc = new environment_mc();
public var player:player_mc = new player_mc();
public var light:Sprite = new Sprite ;
public var key_pressed:int = 0;
public var radius:int = 8;
public var player_speed:int = 2;
public var up,down,left,right:Boolean = false;
public function survival():void
{
addChild(ground);
addChild(environment);
addChild(light);
addChild(player);
player.x = 250;
player.y = 200;
addEventListener(Event.ENTER_FRAME,on_enter_frame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, on_key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, on_key_up);
}
public function on_enter_frame(e:Event):void
{
if (up)
{
environment.y += player_speed;
}
if (down)
{
environment.y -= player_speed;
}
if (left)
{
environment.x += player_speed;
}
if (right)
{
environment.x -= player_speed;
}
while (environment.hitTestPoint(player.x, player.y+radius, true))
{
environment.y++;
}
while (environment.hitTestPoint(player.x, player.y-radius, true))
{
environment.y--;
}
while (environment.hitTestPoint(player.x-radius, player.y, true))
{
environment.x--;
}
while (environment.hitTestPoint(player.x+radius, player.y, true))
{
environment.x++;
}
}
public function on_key_down(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
left = true;
break;
case 38 :
up = true;
break;
case 39 :
right = true;
break;
case 40 :
down = true;
break;
}
}
public function on_key_up(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
left = false;
break;
case 38 :
up = false;
break;
case 39 :
right = false;
break;
case 40 :
down = false;
break;
}
}
}
}