Archived

This topic is now archived and is closed to further replies.

Something Very Strange is going on

This topic is 6387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok. I don''t understand this one bit. //a couple of defines #define DIR_LEFT 0 #define DIR_RIGHT 1 //down here i have a function that checks to see if keys are //pressed and does stuff. //inside this function i have declared a static int LastDir; //if the person hits the left arrow key i make the guy on screen //move and stuff and i also set LastDir = DIR_LEFT; //if they hit the right arrow key LastDir = DIR_RIGHT; These variables are not used anywhere else in the program. I don''t do ANYTHING with them. They are declared and i change their values, but nowhere else are these variables anywhere! Here is the problem: this game is a 2d sidescroller based on tiles. When i move to the right, everything works, but a tile, i think its number 19, changes to whatever value i have for DIR_RIGHT. If i move to the left, that same tile changes to what i have for DIR_LEFT! This makes no sense at all becuase they are not used anywhere else. I have solved this problem by taking the static int LastDir and making it a global int LastDir. Now it doesn''t happen anymore, but this shouldn''t be happpening. I think it must be a problem in VC++ 5 or something. Does anyone know what could possibly be happening????????????

Share this post


Link to post
Share on other sites
It sounds like somehow the variable was in the same memory location as the tile was. I have no idea how that would happen, though.

--TheGoop

Share this post


Link to post
Share on other sites
I remember getting bugs like this when I first picked up C.

It was down to the way I accessed arrays:

int g_Array[100];
int g_Value;

Coming from BASIC programming, I was used to reading arrays from element 1 to 100. But any writes to g_Array[100] above, will be written into g_Value! (that''s if compilers still assign memory to arrays and variables in the way Turbo C did).

Matt



Check out my project at:www.btinternet.com/~Matthew.Bennett

Share this post


Link to post
Share on other sites