简体   繁体   中英

Javascript(gameQuery game) how can I get the next bottom element in the tilemap?

how can I get the next bottom element (id, class) of a selected tile without to loop through the whole tilemap? Eg: my tile $('.gQ_tileType_4') is a rock in the game and this should be fall down if the next bottom tile $('.gQ_tileType_8') a space is. Now I loop throug the whole tilemap and compare the elements against there xy coordinates. But I think is a wrong way, because the game performance is slowing down.

gameQuery gives an unique id to each tiles constructed in the following way:

$.gameQuery.tileIdPrefix+name+"_"+row+"_"+column

This means that if your tilemap is called "foo" then you can access the tile at position (a,b) by writing:

$("#"+$.gameQuery.tileIdPrefi+"foo_"+a+"_"+b);

In the same way you can deduce the position (index-wise) of the tile by parsing it's id. This means that for a given tile you can find it's position and probe specifically the state of the tile just bellow.

How ever I'm not sure using tile-maps is the best solution for this since they are not supposed to change. At the moment it shouldn't cause any problems but I cannot guaranty that in a future version this wont break some functions like collision detection.

In this situation I would simply use sprites to display the elements. To make the game logic implementation of your game faster I would simply store the tiles states in a two-dimensional array as well as a reference to the associated sprite.

This way you can simply work with the array and update the associated sprites if you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM