2016-11-06 8 views
0

私は全く新しいCorona(Lua)です。nilとnumberを比較しようとしました。Lua(Corona Lab)のエラー

ローカル関数gameLoop()

-- create new asteroids 
createAsteroid() 

-- remove asteroids which have been drifted off the screen 
for i = #asteroidsTable, 1, -1 do 
    local thisAsteroid = asteroidsTable [i] 

    if (thisAsteroid.x < -100 or 
     thisAsteroid.x > display.contentWidth + 100 or 
     thisAsteroid.y < -100 or 
     thisAsteroid.y > display.contentHeight + 100) 

    then 

     display.remove(thisAsteroid) 
     table.remove(asteroidsTable) 

    end 

end 

エンド

「数とnilを比較しよう」:ゲームを実行した後、ゲームが数秒後に、私は次のエラーを取得するときまで、完璧に動作しているようです

上記のように、 'thisAsteroid'は 'asteroidsTable = {}'にあります。これは、モジュールの上に変数として定義されており、関数の外に定義されています。あなたの助けのための

ローカルasteroidsTable = {}

ありがとう!

+0

エラーが発生した行の前に 'print'文を使用してみてください。 – hjpotter92

+0

より具体的で、printステートメントの例を挙げてください。 (申し訳ありませんが、私はコーディングに新しいです) – EbrahimB

答えて

0

thisAsteroid.x,thisAsteroid.y,display.contentWidthまたはdisplay.contentHeightnilです。

print(thisAsteroid.x)などを使用して、どれがnilであるかを調べます。

また、問題を見つけるのに役立つエラーメッセージとともに行番号を取得する必要があります。

nilの値を見つけたら、nilにならないようにしなければなりません。そうしないと、比較がnilの値に制限されます。

0

は、テーブルasteroidsTableから最後の要素を取り除くので、命令table.remove(asteroidsTable)documentation

table.remove (list [, pos])

Removes from list the element at position pos, returning the value of the removed element. When pos is an integer between 1 and #list, it shifts down the elements list[pos+1], list[pos+2], ···, list[#list] and erases element list[#list]; The index pos can also be 0 when #list is 0, or #list + 1; in those cases, the function erases the element list[pos].

The default value for pos is #list, so that a call table.remove(l) removes the last element of list l

をlua.orgから

-- create new asteroids 
createAsteroid() 

-- remove asteroids which have been drifted off the screen 
for i = #asteroidsTable, 1, -1 do 
    local asteroid = asteroidsTable [i] 

    if (asteroid.x < -100 or 
     asteroid.x > display.contentWidth + 100 or 
     asteroid.y < -100 or 
     asteroid.y > display.contentHeight + 100) 

    then 
     local asteroidToRemove = table.remove(asteroidsTable, i) 
     if asteroidToRemove ~= nil then 
      display.remove(asteroidToRemove) 
      asteroidToRemove= nil 
     end 
    end 
end 
end 

を試していますが、i番目の要素を削除する必要があります。

コロナforumからテーブルから要素を削除する方法の詳細をご覧ください。

関連する問題