0

オブジェクトのパスを含む配列[15][15]があります。0は壁で、他のものはパスを作成します(1-> 2-> 3-> ...-> end)。これは、450px x 450pxのゲームフィールドを反映しています(30px x 30pxは1つのフィールドです)。

次のようなarray[15][15]の場合:

080000000000000
010111011101110
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
011101110111010
000000000000090

私は得る: map2

私のオブジェクトはある速度*速度で移動します:

void Enemy::move()
{
    this->get_dir();
    this->x += this->velX * this->speed; // velX = -1 is left, 1 is right
    this->y += this->velY * this->speed; // velY = -1 is up, 1 is down
}

get_dir()は、次のように、設定する必要のある方向(速度)をチェックします。

void Enemy::get_dir()
{  
    Point p; // {x, y} struct

    p = this->get_grid(); // it tries to calculate X, Y axis into an array number
    if (p.y < 14 && path_grid[p.y + 1][p.x] - 1 == path_grid[p.y][p.x])
    {
        this->velY = 1;
        this->velX = 0;
        return;
    }
    /* same goes for rest of directions */

    this->velX = this->velY = 0; // if none matched it stops (reached end)
    return;
}

Point Enemy::get_grid()
{
    int x, y;

    for(y = 0;y < 15;y++)
    {
        if (this -> y >= y * 30 && this->y < (y + 1) * 30) break;
    }

    for(x = 0;x < 15;x++)
    {
        if (this -> x >= x * 30 && this->x < (x + 1) * 30) break;
    }

    return make_point(x, y);
}

しかし、お気づきかもしれませんが、これにより、私のオブジェクトは次のようなパスをたどります。 道

左上隅をチェックしているので、右に動かすと原点が変わるはずですが、どうしたらいいのかわかりません。30(オブジェクトビットマップサイズ)を追加しようとすると、うまくいけば上→右隅で止まります。ここでの解決策は何ですか?

4

2 に答える 2

2

質問を誤解しているように見えるため、私の回答のほとんどを削除しました。しかし、私はあなたのget_grid機能が狂っているというこの部分を残しておきます。x数学を使用するだけです(とyは整数であると想定しています):

Point Enemy::get_grid()
{
    return make_point(x / 30, y / 30);
}

さらに、グリッド位置を取得して、各タイルが 30 ピクセルの正方形のタイルの中央に表示する場合は、次のようにします。

int pixelX = gridX * 30 + 15;
int pixelY = gridY * 30 + 15;
于 2013-01-13T22:54:47.797 に答える
1

グリッドから次のオープンポジションを見つけることができるはずですが、バックトラックしないように最後の動きを追跡する必要があります。次に例を示します。

void movePlayer(){
  const GridMap& gridMap = getGridMap(); //returns the 14x14 grid
  const Position& currentPosition = getCurrentPosition(); //returns the current player position
  const Position& previousPosition = _getPreviousPosition(); //private helper func
  //returns a list of position where the current player at the given position can move to

  //at point (x,y) you can move to NORTH,SOUTH,EAST, or WEST one unit, from the gridMap
  //using the currentPosition, return the list of cells that the player at the given position can move to.
  const PositionList& currentMovablePositions = _getMovablePosition(currentPosition,gridMap);
  //return the first matching position that isn't currentPosition or previousPosition
  const Position& nextMovePosition = _getNextMovablePosition(currentMovablePosition,currentPosition,previousPosition);

  this->animateTo( nextMovePosition );

}

于 2013-01-13T23:27:42.133 に答える