dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Need help with wall collision to stop player from moving through

  1. #1
    Member
    Join Date
    Oct 2006
    Posts
    48

    Need help with wall collision to stop player from moving through

    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

    Code:
    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");
                }
            }
        }
    }
    Last edited by humbuged; 09-12-2011 at 05:27 PM.

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Are your player and wall irregular shapes? hitTestObject tests if the bounding boxes of both objects intersect. The bounding box is the rectangle that will cover the entire MovieClip. So if you have a person standing up with his hand reaching forward, the bounding box will be the width of his body and his hand, not just his body.

  3. #3
    Member
    Join Date
    Oct 2006
    Posts
    48
    Quote Originally Posted by 5Five555 View Post
    Are your player and wall irregular shapes? hitTestObject tests if the bounding boxes of both objects intersect. The bounding box is the rectangle that will cover the entire MovieClip. So if you have a person standing up with his hand reaching forward, the bounding box will be the width of his body and his hand, not just his body.
    Ohhh, the wall_mc is a big movieclip with the walls inside it. So if the player_mc is in the middle, it'll keep firing the trace output, right?

    Hmm, how do I fix this? Putting small "wall_mc"s on all sides of the stage? How do I target multiple "wall_mc"s?
    Last edited by humbuged; 09-12-2011 at 09:15 PM.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Yes, if your wall is a donut shape and the player is the hole, hitTestObject will always return true because of the boxes.

    If all you want is to block the sides of the stage like it's some kind of arena then sure, all you need is one wall MovieClip that is the width/height of the stage and you'll have 2 regular walls and 2 walls rotated 90 degrees.

    To check collisions for all 4 walls you'll just have to do hitTestObject 4 separate times. Unless you want to be fancy and put all 4 walls in an array, then use a loop to do the collision detection (not really that fancy at all).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center