0

タイルの衝突方法で問題が発生しています。何らかの理由で、プレーヤーが通過できないタイルを通過できる場合があります。また、理由はよくわかりませんが、スタックすると、オブジェクトを左に移動できますが、左にしか移動できません。以下にいくつかのコードを投稿しました。誰かが私を正しい方向に向けることができれば幸いです。(または、誰かが簡単な解決策を見つけることができればさらに良いことです!) 私のプレーヤー移動メソッドとタイル衝突メソッドは、両方とも更新メソッドで呼び出されます。

マップとタイルの衝突

    map = { {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        {1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
        }

function testTile(x,y)
    if map[y][x + 1] == 1 then
        canRight = true
    end

    if map[y][x + 1] ~= 1 then
        canRight = false
    end

    if map[y][x - 1] == 1 then
        canLeft = true
    end

    if map[y][x - 1] ~= 1 then
        canRight = false
    end

    if map[y + 1][x] == 1 then
        canDown = true
    end

    if map[y + 1][x] ~= 1 then
        canDown = false
    end

    if map[y - 1][x] == 1 then
        canUp = true
    end

    if map[y - 1][x] ~= 1 then
        canUp = false
    end
end

function movePlayer(dt)

    if love.keyboard.isDown("right") and canRight then
        playerX = playerX + 1 * dt
    end

    if love.keyboard.isDown("left") and canLeft then
        playerX = playerX - 1 * dt
    end

    if love.keyboard.isDown("down") and canDown then
        playerY = playerY + 1 * dt
    end

    if love.keyboard.isDown("up") and canUp then
        playerY = playerY - 1 * dt
    end
end
4

2 に答える 2

2

if動かなくなったときに残っている理由は、 の 4 番目のステートメントにタイプミスがあるためですtestTile(x,y)

あなたが書いた

if map[y][x - 1] ~= 1 then
    canRight = false
end

そしてそれはあるべきです

if map[y][x - 1] ~= 1 then
    canLeft = false
end
于 2014-03-03T16:20:32.053 に答える
0

理由は入力ミスです:

if map[y][x - 1] ~= 1 then
    canRight = false;
end

動けない場合に設定canRightします。おそらくコピー/貼り付けエラーであるため、コードをコピー/貼り付けする場合は常に注意してください。これは最も一般的な間違いの 1 つであり、この間違いを検出するのは困難です:Pfalseleft

別の小さな提案: を使用する代わりに

if map[y][x - 1] == 1 then
    canLeft = true;
end
if map[y][x - 1] ~= 1 then
    canLeft = false;
end

あなたが使用できる

else canLeft = false;

コードが短くなり、私の意見ではよりクリーンになります。

于 2014-03-03T16:31:03.413 に答える