Archived

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

user clicks on tile...how?

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

Recommended Posts

ok, my tilebased game has a base grass layer, tree layer, and a water layer so far. and in my game, i want to use the mouse to control where the user moves. so, how can i get my code to realize when the user clicks on any of the tiles on the lpDDStrees surface? obviosly i need to put the code in the WM_LBUTTONDOWN switch.

Share on other sites
There are a few methods you can use. If you have square tiles, or isometric tiles (like Diablo) you can actually do computations to determine where the cursor is.

If you have uneven tiles, or you want to know EXACTLY where the cursor is (for instance, if you have a tree that occludes the background, and when the cursor is over the tree, have it say ''tree'') there are a couple methods:

1) Use a ''detect'' layer. Whenever you draw an item, draw the item silhouetted on another surface in the same place. The color of the item silhouette should be some special detect number. Then, when you hove the mouse, see what number is under the mouse on the special detect surface, and use that number as a reference to what the item is.

ADVANTAGES: Very fast for detecting what the mouse is over
DISADVANTAGES: Slower blitting time, because you have to BLT everything twice.

2) Use near detecting. Whenever you plot a graphic, keep track of the rectangle of the item you plotted, in some big array somewhere. When you sweep the mouse, check if it''s in one of the rectangles you saved. If it IS, check against the sprite''s transparent colorkey in its surface.

DISADVANTAGES: But detecting an item does-- and if you don''t draw your items front to back, you have to make sure that the detect rectangles are sorted, or record a z-order when you try to detect them.

All in all, the second method is faster. If you are trying to detect 3D things, however, it''s all more difficult-- you have to do tests to see if the cursor is inside a triangle, which is both slow AND trying.

-- Goodlife

-----------------------------
Think of your mind as a door on a house. Leave the door always closed, and it's not a house, it's a prison. Leave the door always open, and it's not a house, it's a wilderness-- all the vermin creep in.

Share on other sites
i have isometric tiles. the first method sounds best, i hate arrays. is there a tutorial somewhere that you know of? i didnt see any about mouse functions in the reference section.

Share on other sites
Hehe-- if you''re a programmer and you hate arrays, life is gonna be hell!!

Um, I don''t know of any tutorials... but here''s psuedocode:

BlitGraphic() {
[switch to VISIBLE surface]
blit image
[pick a detect number for this item]
[switch to DETECT surface]
blit image silhouette, using detect number
}

WM_MOUSEMOVE:
[get mouse xpos/ypos]
[get a position with xpos+(ypos*detectsurfacepitch)]
[grab a pixel from the detect surface at this position]
[do whatever you need to do with this detect number, like say ''Thou art hovering thy cursore over a tree, wizard!'']

Hope this helps!

-- Goodlife

-----------------------------
Think of your mind as a door on a house. Leave the door always closed, and it's not a house, it's a prison. Leave the door always open, and it's not a house, it's a wilderness-- all the vermin creep in.

Share on other sites
If you have even tiles, it's easy to do this:
Get the tile co-ordinates from the mouse co-ordinatesfor all your tile layers, from top to bottom:    If there is a tile present on this layer at the given position        check if the mouse is actually pointing at the        sprite (hint - calculate offsets within the tile,        and compare that to the transparent part of the        sprite). If so, this is what you're clicking on,        stop here.repeat for next layerIf you got this far, there was nothing under the mouse.(Usually your bottom layer is fully opaque so you onlyneed to check the layers above conditionally.)

Edited by - Kylotan on 4/25/00 1:37:33 PM

Share on other sites
Divide the mousePos by the size of your tiles.

• 10
• 13
• 52
• 11
• 15