TypeError:错误#2007

时间:2012-09-19 10:50:08

标签: actionscript-3 flash flash-cs3

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

帮助以前的答案仍然无法正常工作.. :(

praticing教程。 当对象git the catcher时,我在代码的这一部分出错了。

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()

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

然后你去做if (objects[i].hitTestObject(player.arrowS.sackC)) {但是如果第一个条件是真的,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))

答案 2 :(得分: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
          }
      }
    }
  }

答案 3 :(得分: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;
        }
    }
}

答案 4 :(得分:0)

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

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

虽然感谢大家的帮助..