2012-09-19 69 views
1

2012年9月20日,格林威治標準時間7:27 PM + 8 ..即使代碼爲「continue」,仍然有相同的錯誤... ... 任何其他建議? :(TypeError:Error#2007

HELP以前的答案仍然心不是工作.. :(

praticing教程。 IM在我的代碼,當對象的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() 
+0

我想,錯誤是由於嘗試訪問對象已被刪除和拼接後。一旦你從舞臺上移除了物體,就打破循環。 – Moorthy

+0

@Frankie Roy Sanqui Niver - 如果你仍然有問題,也許你應該展示更多的代碼。 – BadFeelingAboutThis

+0

我想我的第一條評論是錯的。錯誤是'參數hitTestObject必須是非空的.'意味着你正在提供給'hitTestObject的顯示對象,即player.arrowS.sackC'有時會變成'null'。當輸入框正在聆聽時,您是將它移除還是移動到另一個框架或場景。請檢查 – Moorthy

回答

0

您正在循環訪問一個數組,如果if (objects[i].y > 400) {爲true,那麼您將該元素拼接出數組,因此objects[i]現在爲null。

然後,你去做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)) 
+0

它仍然顯示相同的錯誤.. :( –

+0

)如果您從數組中移除元素,則數組將被刷新。這是下一個索引中的元素向下移動或填充已移除元素的索引位置並且它會一直持續到數組的末尾,所以除非'i = objects.length -1';' – Moorthy

+0

'它不會是'null'對不起,先生,我覺得很難理解..你可以把代碼我必須更換以糾正錯誤?謝謝 –

0

這個錯誤發生在VAL特定變量的ue在運行時爲空。如果hitTestObject值爲null,則檢查不同位置的值。

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 
      } 
     } 
    } 
    } 
+0

先生不知何故它給這個錯誤... 1118:隱式強制的靜態類型的值對象可能無關的類型flash.display:DisplayObject –

+0

@FrankieRoySanquiNiverba:這是因爲他是分配顯示對象存儲在如果所有對象都是影片剪輯,則將'currentObject'聲明爲'var currentObject:MovieClip;',同時將'currentObject = objects [i]'指定爲MovieClip',這樣可以糾正錯誤 – Moorthy

+0

,即使我改變那些..錯誤將不再會是一樣.. 類型錯誤:錯誤#2007:參數hitTestObject必須爲非空 在flash.display使用::的DisplayObject/flash.display一:的DisplayObject :: _則hitTest() 在flash.display :: DisplayObject/hitTestObject() 在adventure_fla :: MainTimeline/moveObject() –

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

'player.arrowS.sackC'是否爲空?請檢查並更新您的評論以更好地幫助您。 – Moorthy

+0

如果你(他低估了我的回答)給出降低投票的理由,那會更好。這會幫助我提高自己。 – Moorthy

+0

我沒有downvote你的答案先生,因爲我不能投票呢.. –

0

先生我得到了答案解決我的問題它不是代碼,但在我爲動畫製作的動畫性格,因爲它包含關鍵幀都有實例名稱sackC:d

我知道這是不專業的方式o不要編碼,但我可以說..即時通訊只是一個初學者..

雖然謝什麼你所有的幫助..

+0

你應該仍然添加continue語句,因爲它們是你需要的代碼,以你期望的方式執行。你說你的sackC追查得很好,你是否在有hitTest的if語句中加入了檢查? – BadFeelingAboutThis

+0

是的,我沒有刪除這些代碼:) tnx –

+1

然後你應該upvote那些誰貢獻。 – BadFeelingAboutThis