# Need help with wall collision to stop player from moving through

**URL:** <https://forum.kirupa.com/t/need-help-with-wall-collision-to-stop-player-from-moving-through/324871>\
**Category:** flash\
**Created:** [September 12, 2011, 9:25pm UTC](https://forum.kirupa.com/t/need-help-with-wall-collision-to-stop-player-from-moving-through/324871 "2011-09-12T21:25:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![humbuged](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@humbuged](https://forum.kirupa.com/u/humbuged)\
**Post date:** [September 12, 2011, 9:25pm UTC](https://forum.kirupa.com/t/need-help-with-wall-collision-to-stop-player-from-moving-through/324871/1 "2011-09-12T21:25:45Z")

</div>

[FONT=arial][COLOR=#222222]Hello 🙂 I’m making a simple test game to learn AS3. I’ve started with learning the codes for keyboard press movements. Then when I got to the wall collision, I’m stumped. The trace output keeps firing regardless if the player touches the wall or not.

I’d appreciate if any of you can help a beginner like me 🙂

I just have 2 movieclip objects on the stage, **player\_mc** and **wall\_mc**.

Here’s my .as code so far

```auto

package {
    import flash.display.*;
    import flash.events.*;

    public class testhit extends MovieClip {

        // initialize arrow variables
        public var leftArrow:Boolean = false;
        public var rightArrow:Boolean = false;
        public var upArrow:Boolean = false;
        public var downArrow:Boolean = false;

        // player speed
        public var speed:Number = 10;

        // game setup
        public function startGame() {
            this.addEventListener(Event.ENTER_FRAME,gameLoop);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyPressedUp);
            stage.addEventListener(Event.ENTER_FRAME,movePlayer);
        }

        // arrow keys pressed
        public function keyPressedDown(event:KeyboardEvent) {
            if (event.keyCode == 37) {
                leftArrow = true;
            } else if (event.keyCode == 39) {
                rightArrow = true;
            } else if (event.keyCode == 38) {
                upArrow = true;
            } else if (event.keyCode == 40) {
                downArrow = true;
            }
        }

        // arrow keys released
        public function keyPressedUp(event:KeyboardEvent) {
            if (event.keyCode == 37) {
                leftArrow = false;
            } else if (event.keyCode == 39) {
                rightArrow = false;
            } else if (event.keyCode == 38) {
                upArrow = false;
            } else if (event.keyCode == 40) {
                downArrow = false;
            }
        }

        // player movement
        public function movePlayer(event:Event) {
            if (leftArrow) {
                player_mc.scaleX = -1;
                player_mc.x -= speed;
            }
            if (rightArrow) {
                player_mc.scaleX = 1;
                player_mc.x += speed;
            }
            if (upArrow) {
                player_mc.y -= speed;
            }
            if (downArrow) {
                player_mc.y += speed;
            }
        }

        // check wall collission loop
        public function gameLoop(event:Event) {

            wallHit();
        }

        // wall collission
        public function wallHit() {

            if (wall_mc.hitTestObject(player_mc)) {
                trace("hit");
            }
        }
    }
}

```

[/COLOR][/FONT]
