0

I have a problem with the below code. I have everything working fine except for the XCoord and YCoord variables. In the ChangePosition method i want to add 25 to the XCoord if its not equal to 50 and then reset XCoord to 0 and add 25 to YCoord if XCoord = 50 and YCoord isnt equal to 50.

What I've found through debugging is that when the ChangePosition method returns the XCoord and YCoord they revert back to 0 when the debugger continues through the switch statement. I think it has something to do with the XCoord and YCoord going out of scope (not 100% sure whether that is that actual issue or not) also is the return XCoord, YCoord syntax legal? I'm using Visual Studio and it's not said that its wrong but I'm just not sure.

I think it must be one of the above 2 reasons why the variables aren't changing. I've tried initialising a struct Coords coords; with the 2 variables in the LoadLevel method and passing the struct into the ChangePosition method but I then have to declare the variables as static ints which then causes an LNK2001 error: I think this has something to do with attempting to adjust the variables using Coords::XCoord += 25. But I am unable to use coords.XCoord as the struct isn't declared in the ChangePosition method so I get an error about the typename not being identified (Something along those lines).

void TileMap::LoadLevel(HINSTANCE hInstance)
{

    LPCSTR szFileName1("..\\Assets\\TILE_01.bmp");
    LPCSTR szFileName2("..\\Assets\\Tile_02.bmp");
    LPCSTR szFileName3("..\\Assets\\Tile_03.bmp");

    Sprite* Tile_01 = new Sprite(); /*Tile_01 is the grass sprite*/
    Sprite* Tile_02 = new Sprite(); /*Tile_02 is the wall sprite*/
    Sprite* Tile_03 = new Sprite(); /*Tile_03 is the end point sprite*/

    int XCoord = 0;
    int YCoord = 0;

    const int Columns = 3;
    const int Rows = 3;

    char MazeMap[Rows][Columns] = {
        {1,2,3},
        {1,2,3},
        {1,2,3}
    };

    for (int x = 0; x < Rows; x++)
    {
        for (int y = 0; y < Columns; y++)
        {
            switch (MazeMap[x][y])
            {
            case GRASS: /*TILE_01*/
                Tile_01->Create(DirectDraw::GetInstance()->GetDDObject(), 25, 25, 0);
                Tile_01->LoadSprite(hInstance, szFileName1, XCoord, YCoord, 25, 25);
                Tile_01->Draw(DirectDraw::GetInstance()->GetBackBuffer(), XCoord, YCoord, 25, 25); //Draw Tile

                ChangePosition(XCoord, YCoord); //Change position
                break;

            case WALL: /*TILE_02*/  
                Tile_02->Create(DirectDraw::GetInstance()->GetDDObject(), 25, 25, 0);
                Tile_02->LoadSprite(hInstance, szFileName2, XCoord, YCoord, 25, 25);
                Tile_02->Draw(DirectDraw::GetInstance()->GetBackBuffer(), XCoord, YCoord, 25, 25); //Draw Tile

                ChangePosition(XCoord, YCoord); //Change position
                break;

            case END: /*TILE_03*/
                Tile_03->Create(DirectDraw::GetInstance()->GetDDObject(), 25, 25, 0);
                Tile_03->LoadSprite(hInstance, szFileName3, XCoord, YCoord, 25, 25);
                Tile_03->Draw(DirectDraw::GetInstance()->GetBackBuffer(), XCoord, YCoord, 25, 25); //Draw Tile

                ChangePosition (XCoord, YCoord); //Change position
                break;
            }
        }
    }

}

/*
This method changes the XCoord and YCoord to pass into the sprite draw method 
so the tile is drawn in the correct position
*/
int TileMap::ChangePosition(int XCoord, int YCoord)
{
    if (XCoord != 50) //if (XCoord != screenWidth) //Change position
    {
        XCoord += 25;
    }
    else if (XCoord == 50 && YCoord != 50) //else if (XCoord == screenWidth && YCoord != screenHeight)
    {
        XCoord = 0;
        YCoord += 25;
    }
    return XCoord, YCoord
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Jemma
  • 13
  • 1
  • 3
  • You can't return two things in one `return` statement. `return XCoord, YCoord` is valid, but it's using the comma operator, so it's equivalent to `return YCoord`. – Barmar May 03 '17 at 23:34
  • And even if you could return multiple values, you're never assigning the result of the function to anything. – Barmar May 03 '17 at 23:35

1 Answers1

1

By default, function parameters are passed by value. This means that when you assign the parameter variables in the function, they have no effect on the caller's variables.

You can change the function to get its parameters by reference. Then the assignments will modify the caller's variables.

void TileMap::ChangePosition(int &XCoord, int &YCoord)
{
    if (XCoord != 50) //if (XCoord != screenWidth) //Change position
    {
        XCoord += 25;
    }
    else if (XCoord == 50 && YCoord != 50) //else if (XCoord == screenWidth && YCoord != screenHeight)
    {
        XCoord = 0;
        YCoord += 25;
    }
}

You don't need to return anything from this function, since it modifies the parameters. I've changed it to a void function.

return XCoord, YCoord; is valid syntax, but it doesn't do what you think. See How does the Comma Operator work

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • To the OP. If you want to return two things, think about using `std::pair` from the standard library ``. Of course, here it is not needed. – cppxor2arr May 03 '17 at 23:46