索引超出范围?

问题描述:

下面是两个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]) 
     ] 
    ); 

我读到的是矢量对象有运行时范围检查(或固定长度检查)除了阵列。这可能是问题吗?

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; 
      } 
     } 
    } 

} 

}
See example

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 
     //... 


Example after updates

+0

之间,如果你让我,所以我会尽力帮you.this代码像c代码。许多索引数组是我知道但想知道的。 –

+0

http://www.slideshare.net/michael.labriola/any-which-array-but-loose这里是一个演示,以帮助您理解数组。 – csomakk

有什么不对您发布的代码,但那些都必须在指数10的对象?也许你应该从0开始看索引9?

你能告诉你如何访问数组/矢量吗?我认为这是错误来自的地方。

+0

错误出现在这一行:if(platformMap [gameObject.bottom] [gameObject.right] == platform) – LuciM

+0

我认为这是因为你正在寻找索引10处的东西,所以当'gameObject.bottom = 10'时会抛出这个错误。做一些检查,以确保它不会查看不存在的索引。 – Michael

+0

我已经更改了碰撞代码并在碰撞测试开始之前添加了一个检查。它工作正常,但现在,gameObject可以在最后一行的环境砖之间自由移动,这是不正确的。您可以在帖子中看到更新。 – LuciM

试试这个代码

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]) 
       ]); 
+0

错误#1034:类型强制失败:无法将2转换为__AS3 __。vec.Vector。<__ as3 __ :: vector>>。 – LuciM

向量长度为​​10,这样你就可以从0到9的 载体含有载体,什么也有LEN访问。 尝试

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

和注释,你只能访问platformMap [N],如果n是0和platformMap.length-1

+0

我不明白的唯一原因是为什么在数组中工作正常?我没有做任何界限检查。这两种情况下的代码都是相似的。为什么矢量如此固执? – LuciM

+0

@LuciM:只是。就好像'Vector's有一个数据类型,'Array's没有,它们只是'Array'的一个更严格的形式。这有时会影响优化,但在我看来,它们最有用的一点是共享代码或自动代码,以防止'Array'允许的奇怪事情发生。你应该使用什么取决于你的代码,以及它的具体或一般性。 – puggsoy

+0

是的,这种方式矢量更快。 – csomakk