- Title: A Simple Trick To Orientating Blocks
- Description: Automatically orientates your blocks depending on the surrounding blocks.
- GM Version: GM8
- Registered: No
- File Type: .gmk
- File Size: 13.3kb
- File Link: Download
- Required Extensions: None
- Required DLLs: None
When creating platform or top-down games, you often want your blocks (or platforms or walls etc.) to show different sprites or tiles depending on the surrounding blocks. Some solutions to this problem that I have seen involve essays of code that repeat place_meeting functions over and over for every different possibility of surrounding blocks. With this simple trick that I will show you though, this is not necessary. If we arrange the sprite subimages in a particular order, we can then check all four sides of each block for another block, and increase the image_index by certain amounts depending on the surrounding block positions, and this results in the correct subimage. Similarly, if we arrange the tiles in a particular horizontal order, and instead increase a variable by certain amounts depending on the surrounding block positions, we achieve the correct tile. This may be difficult to comprehend in writing, so take a look at the following scripts and see if that helps to make it more clear.
The .gmk file shows how to achieve this with both sprites and tiles. You are free to use the scripts/example in your game. When doing so, please note that in order for it to work correctly your sprite subimages must be in the same order as mine (if using sprites) and your tiles must be in the same order as mine and in one single horizontal row (if using tiles).
For sprites, we can use the following script:
//scr_orientate_sprite(objectname,spritewidth,spriteheight); name = argument0; width = argument1; height = argument2; image_speed = 0; image_index = 0; if place_meeting(x-width,y,name) image_index += 8; if place_meeting(x+width,y,name) image_index += 4; if place_meeting(x,y-height,name) image_index += 2; if place_meeting(x,y+height,name) image_index += 1;
For tiles, we can use the following script:
//scr_orientate_tile(objectname,backgroundname,tilewidth,tileheight); name = argument0; bg = argument1; width = argument2; height = argument3; count = 0; if place_meeting(x-width,y,name) count += 8; if place_meeting(x+width,y,name) count += 4; if place_meeting(x,y-height,name) count += 2; if place_meeting(x,y+height,name) count += 1; tile_add(bg,width*count,0,width,height,x,y,depth);
Here is a screenshot of the example in action. On the left we have sprites and on the right we have tiles.
I hope this example is useful! (It better be... it took so damn long to put together! Oh, what the hell, I have no life anyway )