开发者

ActionScript 3 - Walls don't collide properly

开发者 https://www.devze.com 2023-02-12 05:17 出处:网络
For several weeks I\'m been trying to make my topdown game. It went well for some time, but then at some point I wanted to create a scrolling map with walls everywhere. Now, to make it easy to create

For several weeks I'm been trying to make my topdown game. It went well for some time, but then at some point I wanted to create a scrolling map with walls everywhere. Now, to make it easy to create the map (and add more later) I made a class called "Wall" which I will hit test. This works, when it hits, the map must stop scrolling. It does, so good so far.

Now, when the player moves away from the object, I want the map to be able to scroll again, this works too, but now the player can't move to the side the player came from. I know this is because I need to define the sides, where the player enters, in order tell the game which movement must be set to zero at that point.

You can see the code here:

public function AddWalls(player:MovieClip)
{
    WallObjects = new Array();

    for (var i:int = 0; i < this.numChildren; i++)
    {
        var mc = this.getChildAt(i);

        if (mc is Wall)
        {
            var wallobj:Object = new Object();
            wallobj.mc = mc;

            wallobj.leftside = mc.x;
            wallobj.rightside = mc.x + mc.width;

            wallobj.topside = mc.y;
            wallobj.bottomside = mc.y + mc.height;

            wallobj.width = mc.width;
            wallobj.height = mc.height;

            WallObjects.push(wallobj);
        }
    }
}

public function EnableCollisionWithWalls():void
{
    for (var k:int = 0; k < WallObjects.length; k++)
    {
        //if (player.y > WallObjects[k].topside && player.y < WallObjects[k].bottomside && player.x > WallObjects[k].leftside && player.x < WallObjects[k].rightside)
        if (player.hitTestObject(WallObjects[k].mc))
        {
            if (player.x > WallObjects[k].leftside && player.x < WallObjects[k].leftside+15)
            {
                Lefthit = true;
                trace(DebugVar);
                DebugVar++;
                player.x = WallObjects[k].leftside;
                Scroll_x = 0;
            }
            else
                if ( player.x < WallObjects[k].leftside -1 || (player.y > WallObjects[k].leftside ))
                {
                    Lefthit = false;

                }

            if (player.hitTestObject(derp))
            {
                Lefthit = false;
            }
        }
    }
}

public function EnableMovement():void
{
    map.x += Scroll_x;
    map.y += Scroll_y;

    for (var i:int = 0; i < this.numChildren; i++)
    {
        var mc = this.getChildAt(i);
        if (mc is Wall)
        {
            mc.x += Scroll_x;
            mc.y += Scroll_y;
        }
    }
}

public function MovementKeysDown(move:KeyboardEvent):void
{
    var Speed:int = -5;

    switch (move.keyCode)开发者_StackOverflow中文版
    {
        case 37: // venstre knap
            Scroll_x = -Speed;
            break;

        case 38: // op
            Scroll_y = -Speed;
            break;

        case 39: // højre knap
            Scroll_x = Speed;
            if (Lefthit)
            {
                Scroll_x = 0;
            }
            break;

        case 40: // ned
            Scroll_y = Speed;
            break;

        default:
    }
}

public function MovementKeysUp(move:KeyboardEvent):void
{
    switch (move.keyCode)
    {
        case 37:
            Scroll_x = 0;
            break;
        case 38:
            Scroll_y = 0;
            break;
        case 39:
            Scroll_x = 0;
            break;
        case 40:
            Scroll_y = 0;
            break;
        default:
        }
    }

Might be some syntax errors (since I removed some code in this editor). You can see the current version here.

In this version the scroll keeps on going. I did come up with a "fix" for it, by check if the player was 1 pixel away from the movieclip, inside the hit test (which for some reason works, which I guess it shouldn't since it doesn't hit anymore) and then setting the Lefthit to false. However this is not a good solution and if you continue up or down away from the movieclip, you are still not able to go right anymore...

I've been baffled by this for a long time, so I thought it was about time I asked for help. I couldn't find anything on how to control movement in a top-down game, with a scrolling map + wall :/


The simplest (but not most resource friendly) solution (if you anyway have a single storage for walls) is iterating through the walls and instead of using the Flash default hitTest (I don't like the way it works since ActionScript 2) - just check the coordinates and if you see that there's going to be a collision on the next simulation step - handle it according to the game logic.

The most useful optimization for this algorithm is creating a filter/data structure for getting only walls that are near to the player and so can be affected to the test for collisions.

0

精彩评论

暂无评论...
验证码 换一张
取 消