help switching betwen gamestates

Started by
6 comments, last by Weemer 13 years, 8 months ago
hey. I am new at this, but I have a weird problem. I basically want to switch back to Town if I go to a certain point on the Maze map, but for some reason when I go to that point on the map, it gets stuck quickly switching back and forth between the Town Screen and the Maze screen.

Basically it should go: Intro, then Town... then Map if I press 5, then change back to town if I go to the exit( x=0 z=0)

switch(screen)
{
case Intro_Screen:
IntroScreen();
if(g_keys->keyDown[VK_RETURN]==TRUE)
{
g_keys->keyDown[VK_RETURN]=FALSE;
if(InitTown()==TRUE)
screen=Town_Screen;
}
break;

case Town_Screen:
TownScreen();
if(townChoice==5)
{
screen=Map_Screen;
break;
}
break;

case Map_Screen:
MapScreen();
DrawGround(0,0);
if(PlayerpositionX==0 && PlayerposY==0)
screen=Town_Screen;
break;

default:
break;
}

any help would be appreciated. I can send you the full source if it would help you see the problem better. This is probably something really simple I am just not seeing.
Advertisement
I meant to put PlayerposZ. sorry, i had typed that wrong. also "ans" should be &&
That can't be your real code, because wtf is "ans"?

Copy and paste from your real code. And show more context. Where does this switch block take place? What does TownScreen() do? What does MapScreen() do?

And use source tags. This is covered in the sticky thread at the top. The one that says "READ THIS BEFORE POSTING HERE". I wrote "READ THIS BEFORE POSTING HERE" in the title for a reason. The reason is that I, as the moderator of this section, really want people to have read it before posting here.
sorry, my bad. The switch block takes place in the main game loop. TownScreen() basically just shows a .bmp with choices. MapScreen() shows the 3d Maze map. the source code.

void DrawScene(void)													// Draw Our Scene{	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);				// Clear Screen And Depth Buffer	glLoadIdentity ();													// Reset The Modelview Matrix		switch(screen)	{		case Intro_Screen:			IntroScreen();			if(g_keys->keyDown[VK_RETURN]==TRUE)			{				g_keys->keyDown[VK_RETURN]=FALSE;				if(InitTown()==TRUE)				screen=Town_Screen;				}			break;		case Town_Screen:			TownScreen();			if(townChoice==5)			{				screen=Map_Screen;				break;			}			break;		case Map_Screen:			MapScreen();			DrawGround(0,0);                        if(PlayerposX==0 && PlayerposZ==0)                        screen=Town_Screen;			break;				default:			break;	}		glEnable(GL_TEXTURE_2D);	glPrint(300,300,"TEST %i", test);	glDisable(GL_TEXTURE_2D);		glFlush ();															// Flush The GL Rendering Pipeline}
Did you try to debug it? By stepping instruction after instruction you'll most probably very soon see where is the problem, which variable has different value than you expected, which if condition is not doing what you wanted etc.

I cannot see any obvious and common mistake (like = instead of ==) in the code you provided, so the problem can be everything.
I'll give it a shot...

okay, so you're in Map_Screen, and just moved to position (0,0)
case Map_Screen:	MapScreen();	DrawGround(0,0);	if(PlayerposX==0 && PlayerposZ==0)		screen=Town_Screen;	break;


on the next {frame,state,tick,whatever}, you're in Town_Screen
case Town_Screen:	TownScreen();	if(townChoice==5)	{		screen=Map_Screen;		break;	}	break;


somehow, at this same moment, townChoice has the value 5 (I'm guessing that's how you get to the Map_Screen in the first place) and so, by that code, you get transitioned back to Map_Screen.

However, the character position hasn't got a chance to move, so when you get to Map_Screen, the character is still at (0,0), and you get transitioned back to Town_Screen, ad infinitum...

Solution:
case Map_Screen:	MapScreen();	DrawGround(0,0);	if(PlayerposX==0 && PlayerposZ==0)	{		screen=Town_Screen;		townChoice= // set this to some invalid value	}	break;
And that's exactly what debuging would show him :D
I got it fixed, thanks guys. Apparently after debugging I noticed that in Town() it wasn't resetting so it was always going back to townChoice==5. A loop of doom! So I just moved some stuff around and got it working.

Main
switch(screen)	{		case Intro_Screen:			IntroScreen();			stepsTaken=0;			if(g_keys->keyDown[VK_RETURN]==TRUE)			{				g_keys->keyDown[VK_RETURN]=FALSE;				if(InitTown()==TRUE)				screen=Town_Screen;				}			break;		case Town_Screen:			TownScreen();			posX=0;			posZ=0;			stepsTaken=0;			heading=0;			break;		case Map_Screen:			MapScreen();			DrawGround(0,0);			if(posX==0 && posZ<0)				screen=Town_Screen;			break;				default:			break;	}


Town
void TownScreen(){	rot+=2.0f;	glPushMatrix();												//Push the matrix so each block starts at zero	glEnable(GL_TEXTURE_2D);									//enable texturing	glBindTexture(GL_TEXTURE_2D,townText);						//bind the texture to the block(add to this!)	glTranslatef(0,0,-5);		glBegin(GL_QUADS);		glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 2.20f,0);		//draw it counter-clockwise and with texture		glTexCoord2f(0.0f, 1.0f);glVertex3f(-3.0f,2.20f,0);			glTexCoord2f(0.0f, .0f);glVertex3f(-3.0f,-2.20f,0);			glTexCoord2f(1.0f, .0f);glVertex3f(3.0f, -2.20f,0);		glEnd();	glPrint(100,100,"%f",y);//done drawing	glDisable(GL_TEXTURE_2D);									//best to do this(just in case)	glPopMatrix();												//Pop the Matrix	glPushMatrix();	glPushAttrib(GL_CURRENT_BIT);	glTranslatef(-1.75f,1.2f,-5);	glTranslatef(0,y,0);	glRotatef(rot,0,0,1);	glScalef(0.07f,0.07f,0.07f);				glBegin(GL_QUADS);														// Start Drawing A Quad		glColor3f(1,0,0);		  glVertex3f(-1.0f, 1.0f, 0.0f);			// Top Left Of The Quad		glColor3f(0,1,0);		  glVertex3f( 1.0f, 1.0f, 0.0f);			// Top Right Of The Quad		glColor3f(0,0,1);		  glVertex3f( 1.0f,-1.0f, 0.0f);			// Bottom Right Of The Quad		glColor3f(0.5f,0.5f,0.5f);glVertex3f(-1.0f,-1.0f, 0.0f);			// Bottom Left Of The Quad	glEnd();		glEnd();																// Done Drawing The Triangle	glPopAttrib();	glPopMatrix();	if (g_keys->keyDown[VK_DOWN]==TRUE)										//pressing D?	{		if(y>-2.0f)		{			y-= 0.4f;													//rotate(change heading) 90 degreees			g_keys->keyDown[VK_DOWN]= false;		}		else if(y<=-2.0f)		{			y=0;			g_keys->keyDown[VK_DOWN]= false;		}	}	if (g_keys->keyDown[VK_UP]==TRUE)									//pressing D?	{		if(y>=-0.4f)		{			y=-2.0f;													//rotate(change heading) 90 degreees			g_keys->keyDown[VK_UP]= false;		}		else if(y<=2.0f)		{			y+=0.4f;			g_keys->keyDown[VK_UP]= false;		}	}	if(g_keys->keyDown[VK_RETURN]==TRUE)	{		if (y==0)		{			townChoice = 0;		}		if (y==-0.4f)		{			townChoice = 1;		}			if (y==-0.8f)		{			townChoice = 2;		}		if (y==-1.2f)		{			townChoice = 3;		}		if (y==-1.6f)		{			townChoice = 4;		}		if (y==-2.0f)		{			townChoice = 5;			screen=Map_Screen;			y=0;		}	}	switch(townChoice)	{		case Inn:			break;		case Consulate:			break;		case Armory:			break;		case MagicShop:			break;		case AuctionHouse:			break;		case LeaveTown:			break;	}}


thanks for the help guys. I am awful with the debbuger. need more practice.

This topic is closed to new replies.

Advertisement