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
}