1

2012 年 9 月 20 日,格林威治标准时间 + 8 点晚上 7:27.. 即使使用代码“继续;”仍然有同样的错误......还有其他建议吗?:(

帮助以前的答案仍然无效.. :(

练习教程。当对象 git 捕手时,我的这部分代码出现错误。

function moveObject(e:Event):void {
    // cycle thru objects using a for loop
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        objects[i].y += speed;
        objects[i].rotation += speed;
        // if objects leaves the stage
        if (objects[i].y > 400) {
            // remove it from display list
            removeChild(objects[i]);
            // remove it from the array backwards
            // remove it backwards removes all problems when looping through.
            // forward would cause confusion if you removed 5 before 4. 
            objects.splice(i, 1);

        }
        if (objects[i].hitTestObject(player.arrowS.sackC)) {
            if (objects[i].typestr == "good") {
                score += 3;
            } else {
                score -= 5;
                if (score <= 0) {
                    score = 0;
                }
            }
            trace(score);
            updateScore(score);
            removeChild(objects[i]);
            objects.splice(i, 1);
        }
    }
  }

尽管游戏仍在运行,但看到这一点很烦人。这是错误

TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at adventure_fla::MainTimeline/moveObject()
4

5 回答 5

0

当特定变量的值在运行时为空时会发生此错误。如果它变为空,请检查不同位置的 hitTestObject 值。

于 2012-09-19T11:30:22.387 回答
0

您正在遍历一个数组,如果if (objects[i].y > 400) {为真,则将该元素从数组中拼接出来,因此objects[i]现在为空。

然后你去做,if (objects[i].hitTestObject(player.arrowS.sackC)) {但如果第一个 coniditon 为真,则objects[i]不再有值,所以你得到发布的错误。

您想要做的不是跳出循环,而是使用continue关键字,以便循环移动到下一个项目。

if (objects[i].y > 400) {
        // remove it from display list
        removeChild(objects[i]);
        // remove it from the array backwards
        // remove it backwards removes all problems when looping through.
        // forward would cause confusion if you removed 5 before 4. 
        objects.splice(i, 1);
        continue; //abandon the rest of this loop cycle (since objects[i] is now null), and move on to the next loop cycle, so the below code doesn't execute for the current i
}

除了上述之外,您还应该检查您player.arrowS.sackC的不为空:

if(player && player.arrowS && player.arrowS.sackC && objects[i].hitTestObject(player.arrowS.sackC))
于 2012-09-19T17:51:25.710 回答
0

您需要存储对该对象的引用并从该引用中工作

 function moveObject(e:Event):void {
    // cycle thru objects using a for loop

    if(player.arrowS.sackC){
      var currentObject:MovieClip;
      for (var i:int=objects.length-1; i>=0; i--) {
          //move objects down based on speed
          currentObject = objects[i] as MovieClip;
          currentObject.y += speed;
          currentObject.rotation += speed;
          // if objects leaves the stage
          if (currentObject.y > 400) {
              // remove it from display list
              removeChild(currentObject);
              // remove it from the array backwards
              // remove it backwards removes all problems when looping through.
              // forward would cause confusion if you removed 5 before 4. 
              objects.splice(i, 1);
              continue;// go to next iteration of loop
          }
          if (currentObject.hitTestObject(player.arrowS.sackC)) {
              if (currentObject.typestr == "good") {
                  score += 3;
              } else {
                  score -= 5;
                  if (score <= 0) {
                      score = 0;
                  }
              }
              trace(score);
              updateScore(score);
              removeChild(currentObject);
              objects.splice(i, 1);
              continue;// go to next iteration of loop
          }
      }
    }
  }
于 2012-09-19T20:56:25.273 回答
0
function moveObject(e:Event):void {
    // cycle thru objects using a for loop
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        objects[i].y += speed;
        objects[i].rotation += speed;
        // if objects leaves the stage
        if (objects[i].y > 400) {
            // remove it from display list
            removeChild(objects[i]);
            // remove it from the array backwards
            // remove it backwards removes all problems when looping through.
            // forward would cause confusion if you removed 5 before 4. 
            objects.splice(i, 1);
            //once an element is spliced from the array the array is getting refreshed
            //so to avoid accessing empty array(ie, after the last element is removed) in the next if condition, the loop has breaked here
            //Don't worry about return statement as the Enter frame listener function calls till the listener has been removed
            return;
        }
        if (objects[i].hitTestObject(player.arrowS.sackC)) {
            if (objects[i].typestr == "good") {
                score += 3;
            } else {
                score -= 5;
                if (score <= 0) {
                    score = 0;
                }
            }
            trace(score);
            updateScore(score);
            removeChild(objects[i]);
            objects.splice(i, 1);
            return;
        }
    }
}
于 2012-09-20T06:19:17.633 回答
0

先生,我得到了解决我的问题的答案,它不是代码,而是我为角色制作的动画,因为它包含的关键帧都具有实例名称 sackC:D

我知道这不是编码的专业方式,但我能说什么..我只是一个初学者..

虽然谢谢大家的帮助..

于 2012-09-22T03:38:56.040 回答