1

ゲームに衝突を実装しようとしているだけです。

タイルの衝突をテストする方法に問題があるため、衝突が機能していません。

次のような配列があります。

var coll_array = [
[[tile_index],[x],[y]] 
]

タイル インデックスは、使用しているイメージ マップ内のタイルの番号です。x はタイルの x 位置、y は y 位置です。

基本的に tile_index が 0 でない場合、プレイヤーはこのタイルに衝突する必要があります。これが私が達成しようとしていることです。

コード例の更新を参照してください

何らかの理由で衝突した変数がfalseを返し続けますが、理由はわかりませんが、配列の設定方法に関係があると思います。

どんな助けでも大歓迎です。ここにゲームのライブバージョンがあります

ユーザー名 = ゲスト

パスワード = ゲスト。

正しい console.log を確認できるように、新しいゲームを使用してください。

配列がプレーヤー関数に入るときのコンソールログを置いたので、その設定方法を確認できます。

ありがとうございました

アップデート

わかりましたので、コードをもう少しいじると、次のようになります。

function Player()
{
  var sprite = new Sprite(),
   collision_array,
   collided,
   x,
   y,
   w = sprite.width,
   h = sprite.height,
   gameW = canvas.width,
   gameH = canvas.height-192,
block_x,
    block_y,
    block_cx,
    block_cy,
    combined_hw = 32,
    combined_hh = 32,
   player_cx,
   player_cy;

this.keys = [];
// What delay do we want to use between switching sprites (in milliseconds)
this.moveSpeed = 4;
this.player = null;

this.init = function(coll_data){
    collision_array = coll_data;
    console.log(collision_array);
};

this.init_Player = function(pos_X, pos_Y){
  this.player = sprite.load("player");
   x = pos_X;
   y = pos_Y;
};

this.update = function(elapsed) {
    // perform a switch statement on the last key pushed into the array
    // this allows us to always move the direction of the most recently pressed
    // key

    switch (this.keys[this.keys.length - 1])
    {
        case 37:
            // move the player left on the screen
            x -= this.moveSpeed * elapsed;
            break;
        case 38:
            // move the player up on the screen
            y -= this.moveSpeed * elapsed;
            break;
        case 39:
            // move the player right on the screen
            x += this.moveSpeed * elapsed;
            break;
        case 40:
            // move the player down on the screen
            y += this.moveSpeed * elapsed;
            break;
    }
    if (x < 0)
    {
        x = 0;
    }
    if (x >= gameW - w)
    {
        x = gameW - w;
    }
    if (y < 0)
    {
        y = 0;
    }
    if (y >= gameH - h)
    {
        y = gameH - h;
    }

    player_cx = x+(32/2);
    player_cy = y+(32/2);

    collision_array.forEach(function(row) {
        for(var i = 0; i<row.length;i++){
            if(row[i][0] != 0){
                block_x = row[i][1];
                block_y = row[i][2];
                block_cx = block_x+(32/2);
                block_cy = block_y+(32/2);

            }

        }
        collided = Math.abs(player_cx - block_cx)< combined_hw
            && Math.abs(player_cy - block_cy)< combined_hh;
    });

    if(collided)
    {
        console.log("COLLIDED!")
    }
    return {
        'pos_X':x,
        'pos_Y':y
    };
};

this.draw = function() {
    ctx.drawImage(this.player,x, y, w ,h);
};
}

これは機能しますが、配列に保存されている最後の位置、たとえば右下隅でのみ機能します。どこが間違っているのかわかりますか?

4

2 に答える 2