1

Lua (iPad の Codea) で、XY 座標の 4 つのペアがあるプログラムを作成しました。これらは、同じ ID (カウント = カウント + 1) の下のテーブルに配置されます。最初に 1 つのペアのみを使用してコードをテストしたとき、 XY 座標がテーブル内の座標の 1 つ (座標が既に存在する場所) に接触したことを検出します。私はこのビットのコードを使用してこれを行いました:

if (math.abs(xposplayer - posx) < 10) and (math.abs(yposplayer - posy) < 10) and id < (count - 10) then

このコードは、次のループで再生されます。

for id,posx in pairs(tableposx) do
posy = tableposy[id]

これは私が望むように機能しました!

しかし、今私は8つのテーブル(tableposx1 tableposy1、...)を持っていますそして、現在の座標がテーブルのいずれかの座標に触れているかどうかを確認したいので(これまでに)試しました:

for id,posx1 in pairs(tableposx1) do
posy1 = tableposy1[id]
posy2 = tableposy2[id]
posx2 = tableposx2[id]
posy3 = tableposy3[id]
posx3 = tableposx3[id]
posy4 = tableposy4[id]
posx4 = tableposx4[id]

そして、このビットを 4 回 (4 つの現在の座標に対して)

if ((math.abs(xposplayer1 - posx1) < 10) and (math.abs(yposplayer1 - posy1) < 10))
or ((math.abs(xposplayer1 - posx2) < 10) and (math.abs(yposplayer1 - posy2) < 10))
or ((math.abs(xposplayer1 - posx3) < 10) and (math.abs(yposplayer1 - posy3) < 10))
or ((math.abs(xposplayer1 - posx4) < 10) and (math.abs(yposplayer1 - posy4) < 10))
and (id < (count - 10))

しかし、これは常に(ほとんど)真実です。また、テーブルの値が NIL である場合があるため、何かを nil 値と比較できないというエラーがスローされます。

前もって感謝します、ローラン

4

2 に答える 2

2

コピー&ペーストコードを削除することから始めます。、...posy[n]の代わりに次のようなものを使用します。他の場合も同じです。.の代わりに。posy1posy2tableposy[n][id]tableposy1[id]

その後、ループを使用して1行で比較を行うことができます。また、比較をリファクタリングして、比較のnil前にチェックする関数にすることができます。

于 2013-01-10T06:39:23.853 に答える
1

おそらく、テーブルを使用してこれらの値を整理する必要があります。一連の「座標」テーブルを保持する位置のテーブルを使用します。このようにして、forループを使用してすべての座標を反復処理できます。また、テーブル内の各項目が、妥当性をテストするための一般的な関数を記述できる座標ペアを表していることを確認してください。

function GetNewCoords(x_, y_)
    x_ = x_ or 0
    y_ = y_ or 0
    return { x = x_, y = y_}
end

function CoordsAreValid(coords)
    if (coords == nil) return false
    return coords.x ~= 0 or coords.y ~= 0
end

local positions = {}
table.insert(positions, GetNewCoords(5, 10))
table.insert(positions, GetNewCoords(-1, 26))
table.insert(positions, GetNewCoords())
table.insert(positions, GetNewCoords(19, -10))

for _, coords in pairs(positions) do
    if (CoordsAreValid(coords)) then
        print(coords.x, coords.y)
    end
end
于 2013-01-10T15:42:52.423 に答える