0
votes

So I am currently trying to do a game which is almost like a maze. The problem is with the wall collision, once the character hits a wall, I can't get him out anymore, he gets "stuck" no matter what direction I try to take him after the collision. One of the solutions I was thinking would be to whenever the character hits the wall, to "back him up", so that there is no more collision detected. However, when I do that, he goes through the wall in a strange way. Here is my code so you guys can have an idea of what I'm doing:

function keyPressed(event:KeyboardEvent):void
{

    if (event.keyCode == Keyboard.LEFT)
    {
       leftArrow = true;
       if(char.hitTestObject(test))
       {
           leftHit= true;
       } else { 
           leftHit = false;
       }
    }
    if (event.keyCode == Keyboard.RIGHT)
    {
       rightArrow = true;
       if(char.hitTestObject(test))
       {
           rightHit= true;
       } else { 
           rightHit = false;
       }
    }


}

function keyReleased(event:KeyboardEvent):void 
{

    if (event.keyCode == Keyboard.LEFT) 
    {
        leftArrow = false;
    }
    if (event.keyCode == Keyboard.RIGHT)
    {
       rightArrow = false;
    }

}

function walking(event:Event):void {
    if (rightArrow) {
        if(rightHit)
            char.x -= speed;
        else
            char.x += speed;    
    }

    if (leftArrow) {
        if(leftHit)
            char.x += speed;
        else
            char.x -= speed;
    }
}

A big part of this code I actually got from another person asking the same question. Even doing what was suggested in the other topic, the problem still remains. Thank you very much for any help!

1
Is your speed value consistent from frame to frame ? Or is it based on the time passed since the last frame ?prototypical
it's consistent frame to frame. It's basically a constant and I left it's value as 10.theJuls

1 Answers

0
votes

As requested, here is my solution:

 if (rightArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x+speed, MovieClip(root).char.y, true))
                {
                    MovieClip(root).char.x += speed;    
                    x = x-speed; //moving the background
                }
            }

            if (leftArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x-speed, MovieClip(root).char.y, true))
                {
                    MovieClip(root).char.x -= speed;
                    x = x+speed; //moving the background
                }
            }
            if (upArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x, MovieClip(root).char.y-speed, true))
                {
                    MovieClip(root).char.y -= speed;    
                    y = y+speed; //moving the background
                }
            }

            if (downArrow) {
                if (!test.hitTestPoint(MovieClip(root).char.x, MovieClip(root).char.y+speed, true))
                {
                    MovieClip(root).char.y += speed;
                    y = y-speed; //moving the background
                }
            }

It's been a while so some of this stuff I really don't remember, but from what I can see, I check if adding the speed of my character will make it become collided with the wall. If that happens, I do not move the character even if I do seemingly have enough space. I think that is pretty much it.

Hope it helps.