• Create Account

#ActualFantasyVII

Posted 31 July 2012 - 05:47 AM

Hello everyone,

I'm making a 2D Tower defense game.

I have two maps.

The first map draw all the tiles in my game and each tile is 32px.
The second map stores the positions of tiles that I don't want the tower to be build on (in another word tiles that are collidable).

first I'm converting the mouse position into tile position which mean the mouse can only move every 32px not ever 1px.

for example if you move the mouse on the X-axis it will look like this X=32, X=64, X=96 etc... same goes for the Y-axis.

then I have an if statment that check if the player mouse position is equal to the collidable tiles which are stores in a list. so if its true I set a bool value call Collision to true.

like so

MousePosition.X = (int)MousePos.X / 32;
MousePosition.Y = (int)MousePos.Y / 32;

// for example if MousePosition which is (128, 64) == Map.CollisionPosition[0] which is (128, 64) collision = true
if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else
collision = false;


no I have 66 tiles to check for. So natuarlly I did a for loop to check if the mouse position is equal to the collidable tile position like to.

for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
collision = true;
else
collision = false;


but that didn't work at all. it only check the last two tiles position in the list and sometimes it work and sometimes it doesn't.

Now I found a solution but its not good at all. If I wanted this to work I had to do an if statement to check for each tile. So I would do 65 if statement to check if collision occur or not !!!

if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[1])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[2])
collision = true;
............................................
else if (MousePosition * 32 == Map.CollisionPosition[65])
collision = true;
else
collision = false;


Map.CollisionPosition is public static in Map class

so my question is why does this work when I do 65 if statements and doesn't when I try to use a for loop. FYI this is all done in the Update method of the game.

#2FantasyVII

Posted 31 July 2012 - 05:45 AM

Hello everyone,

I'm making a 2D Tower defense game.

I have two maps.

The first map draw all the tiles in my game and each tile is 32px.
The second map stores the positions of tiles that I don't want the tower to be build on (in another word tiles that are collidable).

first I'm converting the mouse position into tile position which mean the mouse can only move every 32px not ever 1px.

for example if you move the mouse on the X-axis it will look like this X=32, X=64, X=96 etc... same goes for the Y-axis.

then I have an if statment that check if the player mouse position is equal to the collidable tiles which are stores in a list. so if its true I set a bool value call Collision to true.

like so

MousePosition.X = (int)MousePos.X / 32;
MousePosition.Y = (int)MousePos.Y / 32;

// for example if MousePosition which is (128, 64) == tile.Position which is (128, 64) collision = true
if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else
collision = false;


no I have 66 tiles to check for. So natuarlly I did a for loop to check if the mouse position is equal to the collidable tile position like to.

for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
collision = true;
else
collision = false;


but that didn't work at all. it only check the last two tiles position in the list and sometimes it work and sometimes it doesn't.

Now I found a solution but its not good at all. If I wanted this to work I had to do an if statement to check for each tile. So I would do 65 if statement to check if collision occur or not !!!

if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[1])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[2])
collision = true;
............................................
else if (MousePosition * 32 == Map.CollisionPosition[65])
collision = true;
else
collision = false;


Map.CollisionPosition is public static in Map class

so my question is why does this work when I do 65 if statements and doesn't when I try to use a for loop. FYI this is all done in the Update method of the game.

#1FantasyVII

Posted 31 July 2012 - 05:44 AM

Hello everyone,

I'm making a 2D Tower defense game.

I have two maps.

The first map draw all the tiles in my game and each tile is 32px.
The second map stores the positions of tiles that I don't want the tower to be build on (in another word tiles that are collidable).

first I'm converting the mouse position into tile position which mean the mouse can only move every 32px not ever 1px.

for example if you move the mouse on the X-axis it will look like this X=32, X=64, X=96 etc... same goes for the Y-axis.

then I have an if statment that check if the player mouse position is equal to the collidable tiles which are stores in a list. so if its true I set a bool value call Collision to true.

like so

MousePosition.X = (int)MousePos.X / 32;
MousePosition.Y = (int)MousePos.Y / 32;

// for example if MousePosition which is (128, 64) == tile.Position which is (128, 64) collision = true
if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else
collision = false;


no I have 66 tiles to check for. So natuarlly I did a for loop to check if the mouse position is equal to the collidable tile position like to.

for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
collision = true;
else
collision = false;


but that didn't work at all. it only check the last two tiles position in the list and sometimes it work and sometimes it doesn't.

Now I found a solution but its not good at all. If I wanted this to work I had to do an if statement to check for each tile. So I would do 65 if statement to check if collision occur or not !!!

if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[1])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[2])
collision = true;
............................................
else if (MousePosition * 32 == Map.CollisionPosition[65])
collision = true;
else
collision = false;


so my question is why does this work when I do 65 if statements and doesn't when I try to use a for loop. FYI this is all done in the Update method of the game.

PARTNERS