# \[AS3\] Character Still; Background Moving

**URL:** <https://forum.kirupa.com/t/as3-character-still-background-moving/317126>\
**Category:** programming\
**Created:** [December 11, 2010, 3:33am UTC](https://forum.kirupa.com/t/as3-character-still-background-moving/317126 "2010-12-11T03:33:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GexetGuy](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GexetGuy](https://forum.kirupa.com/u/GexetGuy)\
**Post date:** [December 11, 2010, 3:33am UTC](https://forum.kirupa.com/t/as3-character-still-background-moving/317126/1 "2010-12-11T03:33:18Z")

</div>

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/](http://www.emanueleferonato.com/2010/06/07/create-a-survival-horror-game-in-flash-as3-version/)

My alteration:

```auto

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;
            }
        }
            }
}

```
