Thread: Vectors, iterators and loops

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    Vectors, iterators and loops

    I have a tile map each tile has its own class, each class is stored in a two dimensional array, one of the things I store in the Tile class is how many ships are present each ship is assigned a Unique number to identify it. If there are more then 1 ship in a given Tile the program knows what one you wish to move (I pass the identifying number to the function). I have this piece of code that is supposed to find this ship that the player clicked on a move it to where they clicked however it only ever moves the first ship in the vector.

    Code:
    vector<int>::iterator MoveShip = Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.begin();
    for(; *MoveShip < Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.size();) {
        if(Tile[OldMouseMapX][OldMouseMapY]->ShipsHere[*MoveShip] == IShipID) {
            MoveShip = Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.erase(MoveShip);
            Tile[MouseMap(X)][MouseMap(Y)]->ShipsHere.push_back(*MoveShip);
        }
        else {
            ++MoveShip;
        }
    }
    I know its passing along the right ship number and it is looping until it finds the ship but it will only ever move the first ship contained in the vector.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In the for-loop you're comparing ID's with vector positions.
    could it be that you actually want
    Code:
    for(; MoveShip != Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.end();) {
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    Thanks Kurt but that didn't fixed the problem it works just as it always did

Popular pages Recent additions subscribe to a feed