【问题标题】:TypeError: Error #2007: Parameter hitTestObject must be non-null. WhyTypeError:错误 #2007:参数 hitTestObject 必须为非空。为什么
【发布时间】:2013-08-19 22:12:05
【问题描述】:

所以我试图制作一个子弹船 hitTestObject ,但我不知道为什么代表 bullets.length 的变量不会改变。

所以错误来自这个函数

function doShips() {
      trace("bcount :" + bcount)
      trace("_bulletsArray length:" + _bulletsArray.length)
      for (var i:int = shipArray.length - 1; i >= 0; i--) {
                shipArray[i].moveDown() //what the code in the Ship and Ship2 class does -> only: this.y += 3
                for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) {
                          //if the bullet is touching the ship

                                   while (shipArray[i].hitTestObject(_bulletsArray[bcount])) {
                                    //if we get here it means there`s is a collision

                                     removeChild(_bulletsArray[bcount]);
                                    _bulletsArray.splice(bcount,1);
                                    removeChild(shipArray[i]);
                                    shipArray.splice(i,1);
                          }
                }
      }

}

在此之前,我还有一个射击功能,可以发射子弹并将它们放入 _bulletsArray。

当痕迹出现时,它会显示: 当我不射击子弹时,它给了我这个

_bulletsArray length: 0
bcount: 0

当我拍摄时它给了我这个:

bcount: 0
_bulletsArray length: 1

或

 bcount: 0
_bulletsArray length: 2

那么为什么当 _bulletsArray 改变时 bcount 不改变,当我在 for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) { 中告诉它这样做时 更糟糕的是 - 当我将“bcount”数据类型为数字“bcount:Number”时,它给了我 NaN

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    由于您从 length-1 向下迭代到 0,因此当循环退出时 bcount 的值将为 0,因此这就是跟踪将显示的值。

    空参数错误是因为您删除了碰撞的船和子弹,然后在 while 语句的下一个循环中,您在与第一次相同的索引处测试数组中的对象。如果任何对象碰巧位于数组的末尾,则该位置现在将为空。 要解决此问题,请将 while 循环替换为 if 语句。

    一般来说,当您在迭代数组元素时更改数组时必须小心,因为对象的位置会发生变化,并且您必须(就像您所做的那样)从最后一个元素迭代到第一个元素。

    【讨论】:

    • 另外,一个通用的经验法则:如果你要迭代一个数组并可能删除一些数组元素,你应该总是向后迭代数组:for (var i:int = length - 1; i >= 0; i--)跨度>
    【解决方案2】:

    尝试在循环内追踪bcount。你在循环之前跟踪它。

    function doShips() {
      trace("bcount :" + bcount)
      trace("_bulletsArray length:" + _bulletsArray.length)
      for (var i:int = shipArray.length - 1; i >= 0; i--) {
                shipArray[i].moveDown() //what the code in the Ship and Ship2 class does -> only: this.y += 3
                for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) {
                          //if the bullet is touching the ship
    
                          while(shipArray[i].hitTestObject(_bulletsArray[bcount])) {
                                    //if we get here it means there`s is a collision
    
                                     removeChild(_bulletsArray[bcount]);
                                    _bulletsArray.splice(bcount,1);
                                    removeChild(shipArray[i]);
                                    shipArray.splice(i,1);
                                    trace("bcount: " + bcount);
                          }
                }
      }}
    

    试试看。

    【讨论】:

    • 试试我上面的代码,你会看到 bcount 值一直在变化。通过你的循环。
    • 您在 WhileLoop 中,该循环位于嵌套在另一个父 ForLoop 中的 ForLoop 中。因此,当您在 whileLoop 中检查 hitTestObject 时,您会从显示列表和相应的数组中删除项目。所以数组中的项目:shipArray & _bulletsArray 被删除。当 For 循环再次运行时,它无法在该索引处找到要检查的对象。所以你得到 Parameter hitTestObject must be non-null for shipArray[i].hitTestObject(_bulletsArray[bcount]).
    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2013-02-17
    • 1970-01-01
    • 2017-12-26
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多