1

下面是两个 2D 对象,Array 和 Vector。如您所见,两者中描述的信息是相同的。我的游戏使用 Array 对象可以完美运行,但使用 Vector 会引发以下错误:

[Fault] exception, information=RangeError: Error #1125: The index 10 is out of range 10. 

var _platformMap:Array = [
       [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
       [00, 00, 10, 10, 10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10, 00, 00, 00, 10],
       [10, 10, 10, 00, 00, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
       [10, 10, 00, 00, 00, 00, 00, 10, 10, 10],
       [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
       [00, 00, 00, 00, 00, 00, 00, 00, 00, 00]
     ];

var _platformMap:Vector.<Vector.<int>> = Vector.<Vector.<int>>(
        [
            Vector.<int>([10,10,10,10,10,10,10,10,10,10]), 
            Vector.<int>([00,00,10,10,10,10,10,10,10,10]), 
            Vector.<int>([10,10,10,10,01,01,01,10,10,10]), 
            Vector.<int>([10,10,10,00,10,10,10,10,01,10]), 
            Vector.<int>([10,10,10,00,10,10,01,01,01,00]), 
            Vector.<int>([10,10,10,10,01,01,01,01,01,10]), 
            Vector.<int>([00,00,00,00,00,10,10,10,10,10]), 
            Vector.<int>([00,00,00,00,00,00,00,00,00,00])
        ]
    );

我读到 Vector 对象除了数组之外还有运行时范围检查(或固定长度检查)。这可能是问题吗?

public class TileCollisionController
{
    private var _softPlatformOpen:Boolean = true;
    private var _elevatorOpen:Boolean = true;

    public function TileCollisionController()
    {}

    public function platformCollision(gameObject:TileModel, platformMap:Vector.<Vector.<int>>, maxTileSize:uint, platform:uint):void
    {
        var overlapX:Number;
        var overlapY:Number;
        //check top-left corner
        if (platformMap[gameObject.top][gameObject.left] == platform)
        {
            overlapX = gameObject.xPos % maxTileSize;
            overlapY = gameObject.yPos % maxTileSize;
            if (overlapY >= overlapX)
            {
                if (gameObject.vy < 0 && platformMap[gameObject.bottom][gameObject.left] != platform)
                {
                    //Collision on top side of the object
                    gameObject.setY = gameObject.mapRow * maxTileSize;
                    gameObject.vy = 0;
                }
            }
            else
            {
                //Collision on left side of the object
                gameObject.setX = gameObject.mapColumn * maxTileSize;
                gameObject.vx = 0;
            }
        }
        //check top-right corner
        if (platformMap[gameObject.top][gameObject.right] == platform)
        {
            overlapX = maxTileSize - ((gameObject.xPos + gameObject.width) % maxTileSize);
            overlapY = gameObject.yPos % maxTileSize;
            if (overlapY >= overlapX)
            {
                if (gameObject.vy < 0 && platformMap[gameObject.bottom][gameObject.right] != platform)
                {
                    gameObject.setY = (gameObject.mapRow * maxTileSize);
                    gameObject.vy = 0;
                }
            }
            else
            {
                //Collision on right
                gameObject.setX = (gameObject.mapColumn * maxTileSize) + ((maxTileSize - gameObject.width) - 1);
                gameObject.vx = 0;
            }
        }
        //check bottom-left corner
        if (platformMap[gameObject.bottom][gameObject.left] == platform)
        {
            overlapX = gameObject.xPos % maxTileSize;
            overlapY = maxTileSize - ((gameObject.yPos + gameObject.height) % maxTileSize);
            if (overlapY >= overlapX)
            {
                if (gameObject.vy > 0 && platformMap[gameObject.top][gameObject.left] != platform)
                {
                    //trace("Collision on bottom");
                    //Collision on bottom
                    gameObject.setY = (gameObject.mapRow * maxTileSize) + (maxTileSize - gameObject.height);
                    gameObject.vy = 0;
                    gameObject.jumping = false;
                }
            }
            else
            {
                //trace("Collision on bottom left");
                //Collision on left
                gameObject.setX = gameObject.mapColumn * maxTileSize;
                gameObject.vx = 0;
            }
        }
        //check bottom-right corner
        if (platformMap[gameObject.bottom][gameObject.right] == platform)
        {
            overlapX = maxTileSize - ((gameObject.xPos + gameObject.width) % maxTileSize);
            overlapY = maxTileSize - ((gameObject.yPos + gameObject.height) % maxTileSize);
            if (overlapY >= overlapX)
            {
                if (gameObject.vy > 0 && platformMap[gameObject.top][gameObject.right] != platform)
                {
                    //trace("Collision on bottom right");
                    //Collision on bottom
                    gameObject.setY = (gameObject.mapRow * maxTileSize) + (maxTileSize - gameObject.height);
                    gameObject.vy = 0;
                    gameObject.jumping = false;
                }
            }
            else
            {
                //trace("Collision on right");
                //Collision on right
                gameObject.setX = (gameObject.mapColumn * maxTileSize) + ((maxTileSize - gameObject.width) - 1);
                gameObject.vx = 0;
            }
        }
    }

}


例子

    public function platformCollision(gameObject:TileModel, platformMap:Vector.<Vector.<int>>, maxTileSize:uint, platform:uint):void
    {
        var overlapX:Number;
        var overlapY:Number;
        if(gameObject.bottom < platformMap.length && gameObject.right < platformMap[0].length)
        {
        //check top-left corner
        //...


更新后的示例

4

3 回答 3

2

您发布的代码没有任何问题,但是它们都没有索引 10 处的对象?也许您应该查看索引 9,因为它从 0 开始?

你能展示你是如何访问数组/向量的吗?我认为这就是错误的来源。

于 2013-01-10T10:51:27.860 回答
0

试试这个代码

var platformMap:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(2);(
                [
                    Vector.<int>([10,10,10,10,10,10,10,10,10,10]), 
                    Vector.<int>([00,00,10,10,10,10,10,10,10,10]), 
                    Vector.<int>([10,10,10,10,01,01,01,10,10,10]), 
                    Vector.<int>([10,10,10,00,10,10,10,10,01,10]), 
                    Vector.<int>([10,10,10,00,10,10,01,01,01,00]), 
                    Vector.<int>([10,10,10,10,01,01,01,01,01,10]), 
                    Vector.<int>([00,00,00,00,00,10,10,10,10,10]), 
                    Vector.<int>([00,00,00,00,00,00,00,00,00,00])
                ]);
于 2013-01-10T11:03:53.910 回答
0

向量长度为​​ 10,因此您可以访问 0 到 9。向量包含向量,也有一个 len。尝试

trace("maxnumallowed", platformMap.length-1)

请注意,如果 n 介于 0 和 platformMap.length-1 之间,您只能访问 platformMap[n]

于 2013-01-10T11:47:14.067 回答