Sign in to follow this  
Axesor

Moving across placed tiles

Recommended Posts

Okay. I am working on a megaman battle network fan game and I'm stuck with a little problem: the overworld. I've created arrays to place tiles wherethe X and Y coordinances are which works fine. My character also moves fine, but he won't stay in the given coordinances of the tiles. Heres the coordinances:
	/*Tryout area.*/
	MapData1.type = 2;
	//bottom layer
	MapData1.X[0] = -4;  	MapData1.X[1] = -4; 
	MapData1.Y[0] = -2;     MapData1.Y[1] = -1;
	//middle layer
	MapData1.X[2] = -4;  	MapData1.X[3] = -3; 
	MapData1.Y[2] = 0;      MapData1.Y[3] = 0;
	MapData1.X[4] = -2;  	MapData1.X[5] = -1; 
	MapData1.Y[4] =  0;     MapData1.Y[5] = 0;
	//top layer
	MapData1.X[6] = -1;  	MapData1.X[7] = -1; 
	MapData1.Y[6] = 1;      MapData1.Y[7] = 2;

The tiles are then place perfectly, but I want my character to only wlak across these tiles. I've used this function to make sure this happens. It works, but not well at all:

void Move(bool x, bool y, bool neg)
{
  static int ix=0; static int iy=0;
  
	  if(x && neg)
	  {
		  ix +=1;
	   if(MapData1.X[ix] < MapData1.X[ix+1] && walkx >= MapData1.X[ix])
	   {
           b_walkx1 = true;
	   }
	   else
	   {
		   b_walkx1 = false;
	   }

	  }

	   else if(x && !neg)
	  {
		  ix-=1;
	   if(MapData1.X[ix] > MapData1.X[ix-1] && walkx <= MapData1.X[ix])
	   {
		   b_walkx2 = true;
	   }
	   else
	   {
		   b_walkx2 = false;
	   }
	  }

	   if (y && neg)
	  {
		   iy +=1;
			//The iy stage, or the index of y IofY
	    if(MapData1.Y[iy] < MapData1.Y[iy+1] && walky >= MapData1.Y[iy])
	   {
		   b_walky1 = true; 
	   }
	   else
	   {
		   b_walky1 = false;
	   }
	   }

	   else if(y && !neg)
	   {
		   iy-=1;
	   if(MapData1.Y[iy] > MapData1.Y[iy-1] && walky <= MapData1.Y[iy])
	   {

		   b_walky2 = true;
	   }
	   else
	   {
		   b_walky2 = false;
	   }
	   }

	   if(ix<=0){ix=0;}if(ix>=7){ix=7;}
	   if(iy<=0){iy=0;}if(iy>=7){iy=7;}
  
}

By looking at this, I'm trying to check to see if the next space the character will move in will be a tile or not. If so, it moves ahead by setting the b_walk*(x,y|1,2) to true or false. My results are the haracter moves, but sometimes won't go back to the last spot or the character moves too far and keeps going. I really need help with this since I've been working on this for a month now and i'm sure one of you esquisite programmers who are far more better than me would kindly help. If you do help solve this problem, I will add you to the credits. ~Thank you ~Axesor (website url for project: www.atfreeforum.com/pcn)

Share this post


Link to post
Share on other sites
Hello.
I don't understand wholy a procedure.
What is i.e walkx and walky?
But there are certain problems in your example.

static int ix=0; static int iy=0;

If just one entity (player or enemy) use that procedure, that is ok.
But since that is not part of class, I doubt it.
Using static variables in game, where two or more entities use same procedure can be very dangerous. Personalize it for player or enemy, or for anything that use such a routine.

Another one mistake is that Move procedure will work correctly ONLY if:
Move(x,0, true)
Move(x,y, false)
Move(0,y, true)
Move(0,y, false)

because if you call it with Move(x,y, true) program will execute
block of (x&neg) and later (y&neg) too.

Also you are using four bool variables to determine which direction player moved:
b_walkx1, b_walkx2, b_walky1, b_walky2
Why not use one POINT variable? Like:
POINT m_ptDirection;
m_ptDirection.x = -1; // left
m_ptDirection.x = 1; // right
m_ptDirection.y = -1; // up
m_ptDirection.y = 1; // down

m_ptDirection.x = 0;
m_ptDirection.y = 0; // not moving

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this