• Popular Now

• 13
• 18
• 19
• 27
• 10

Archived

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

help with stupid question

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

Recommended Posts

high, i have an array of WALLS which i check for in collision...anyway does anyone know how to not hard-code this? i just dont understand where i could put a for loop in there...i mean where would the ending curly brace for it go? i just dont know
	for(tilecoordx; ((tilecoordx * 32) <= (tempx + 31)); tilecoordx++) // checking two x's for the first y

{

if (map[tilecoordy][tilecoordx] == WALLS[0] || map[tilecoordy][tilecoordx] == WALLS[1] ||
map[tilecoordy][tilecoordx] == WALLS[2] || map[tilecoordy][tilecoordx] == WALLS[3] ||
map[tilecoordy][tilecoordx] == WALLS[4] || map[tilecoordy][tilecoordx] == WALLS[5] ||
map[tilecoordy][tilecoordx] == WALLS[6] || map[tilecoordy][tilecoordx] == WALLS[7] ||
map[tilecoordy][tilecoordx] == WALLS[8] || map[tilecoordy][tilecoordx] == WALLS[9])
{
collision = true;
break;
}
else if .......

you see how i have hard coded WALLS[0], WALLS[1], ETC. i dont want to hard code this.. i want to be able to change how many elements are in WALLS and not have to come back here and screw with anything... i want to be able to check each element of WALLS.... obviously this wont work..

for(tilecoordx; ((tilecoordx * 32) <= (tempx + 31)); tilecoordx++) // checking two x's for the first y

{
//if(map[tilecoordy][tilecoordx] >= WALLS[0] && map[tilecoordy][tilecoordx] <= WALLS[MAX_WALLS - 1] )

for(int i = 0; i < MAX_WALLS; i++)
{
if(map[tilecoordy][tilecoordx] == WALLS[i])
{
collision = true;
break;
}
} //this wont work,

else if....

this is what i came up with just now:
for(tilecoordx; ((tilecoordx * 32) <= (tempx + 31)); tilecoordx++) // checking two x's for the first y

{

if(map[tilecoordy][tilecoordx] >= WALLS[0] && map[tilecoordy][tilecoordx] <= WALLS[MAX_WALLS - 1] )
{
collision = true;
break;
}

//else if it wasnt a wall, we check  if it was the player using this function.

//if it was, we switch that tile to an empty space, effectively making him eat the food

else if...

this "should" work, because walls[] is an array of consecutive numbers, ie walls[0] is 22, walls[1] is 23, etc. but i dont want to use this because it doenst seem very safe, for some reason i just dont trust it. does anyone know how i could do this? thanks for any help... [edited by - graveyard filla on March 26, 2004 1:27:39 AM]

Share on other sites
Why do you say your second solution wont work. Looks fine to me.
And the third one looks fine too...
For your solution the third is probably the best fit. Theres no reason to check against each wall when WALL[] is an array of consecutive integers.

for(tilecoordx;((tilecoordx * 32)<=(tempx + 31));tilecoordx++)	{   	       int MaxWallVal = WALLS[MAX_WALLS-1];               int MinWallVal = WALLS[0];               int MapVal = map[tilecoordy][tilecoordx];                              if ((MapVal>=MinWallVal) && (MapVal<=MaxWallVal))		{						collision = true;			break;    			}

Share on other sites
still dealing with this, eh? :-)

Well, here's something that *might* work.

Are you still trying to use enums for your walls? Or is WALL a class of some kind?

If it's an enum, an enum is basically a fancied up integer. As such, it can be converted. Thus, reserve yourself a hunk of space in your enum declaration (say 0-50, even if you only have 10 types right now). When comparing, you can (int)ify your enums and test a range like this:

if (    int(map[tilecoordy][tilecoordx]) >= int(OBSTACLE_WALL_1)    &&    int(map[tilecoordy][tilecoordx]) <= int(OBSTACLE_WALL_10)    ) { do something }

WARNING!!! the whole point of having an enum is the flexability of not having to worry about what number they attach themselves to. You should be able to rearrange the declaration and not have it make much difference. If you use the above code, you'll have to "set in stone" you enum and your code will have to know how many WALL types you have in there. But to be sure, like i said, reserve a block of space to grow into.

enum Thingoids {   THING1=0,   THING2,   THING3,   THING4,   OTHER_THING=50   }

They other way to go about it is, if your WALL is a class, inherit your different walltypes from "class Wall". That way, they all behave like a wall, but differently.

Yet another hokey way to do it is to have WALL be a struct with a type and subtype:

struct Wall {
primary_type prime;
sub_type sub
}

where "primary_type" and "sub_type" are enums. but that get's ugly in a hurry.

[edited by - leiavoia on March 26, 2004 1:57:44 AM]