Consider this.
I have a Map object composed out of Tile objects, each Tile holds some Tile specific data like id, texture and some other, for this topic irrelevant, properties.
I also have a Main class, which holds the 'game' loop and in which the Map (and it's tiles) are drawn to the screen. Now I would like to make the Tiles clickable, to add an eventhandler to them, how is this best accomplished?
Tiles clickable
-
- Posts: 17
- Joined: Sat Mar 24, 2007 5:03 pm
- Location: Holland
Tiles clickable
Common sense is what tells you the world is flat.
Assuming that each tile is derived from ISceneNode you should be able to use the scene collision manager to see which one is clicked on. Depending on the camera view, you may have to write some similar code yourself, but if you are looking down at the tiles it should work pretty well.
If your tiles are not scene nodes, then you have to write the logic to figure out which one is clicked yourself, because the collision manager only knows how to do picking with scene node bounding boxes.
Travis
If your tiles are not scene nodes, then you have to write the logic to figure out which one is clicked yourself, because the collision manager only knows how to do picking with scene node bounding boxes.
Travis
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
first dont put the map build function or the creation code of the map in the game loop cuz this will make it create the map several types on top of each other making big problems
second if u want to make click able tiles here what u have to do:
first make a mouse handler so to know when the left mouse button clicks
here is a mouse and key handler:
second u must check when the left mouse clicks on a tile and dont forget to name the tiles "tile" (in the game loop):
so if u want to change the selected tile change it by "selectedtile" node
i hope that was easy!!
second if u want to make click able tiles here what u have to do:
first make a mouse handler so to know when the left mouse button clicks
here is a mouse and key handler:
Code: Select all
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
up = event.KeyInput.PressedDown;
break;
case KEY_KEY_S:
down = event.KeyInput.PressedDown;
break;
case KEY_KEY_A:
left = event.KeyInput.PressedDown;
break;
case KEY_KEY_D:
right = event.KeyInput.PressedDown;
break;
}
return true;
}
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
switch (event.MouseInput.Event) {
case EMIE_LMOUSE_LEFT_UP:
lmouse = true;
return true;
break;
case EMIE_RMOUSE_LEFT_UP:
rmouse = true;
return true;
break;
default:
return false;
}
return true;
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
//int
IrrlichtDevice *device =
createDevice(EDT_DIRECT3D9,dimension2d<s32>(800, 600), 32,
false, false, false, &receiver);
Code: Select all
iscenenode * node;
iscenenode * selectedtile;
if(lmouse==true)
{
node=smgr->getSceneCollisionManager()->getSceneNodeFromScreenCoordinatesBB(mousepos, 0);
if(stringc(node->getName())=="tile")
{
selectedtile=node;
r=1;
}
else
{
selectedtile=0;
r=0;
}
}
i hope that was easy!!