-No need for wall objects. Saves processing time as long as you don't have too many instances with this code active at a time, and keeps deactivated enemies from falling trough and get stuck halfway into, deactivated walls. This code is cut and sliced for those of you who have platformer games and use deactivation for purposes like having the game run quickly or making enemies stay until the player is near: it makes things with gravity behave properly as soon as they get activated again (the tiles is always there, which means enemies can't e.g. fall trough deactivated floors); and when you use deactivation, only few instances will remain and execute this rathter expensive code.
The code presumes a few things:
1) The game must be a platformer.
2) Horizontal movement must be handled by custom variable 'xspeed'.
3) Object needs variable 'onground'. Set xspeed and onground to 0 in create event.
4) Code is not compatible with uphills, only with rectangular tiles. All tiles are treated like rectangles, even if they happen to have transparent parts making them triangles.
5) The tile layer used for collisions might not contain decoration tiles (like e.g. bushes, hanging chains), or the instance will collide with them as well.
6) Place this code in the End Step event. For best performance, have code that deactivates stuff in the Begin Step event of the controller object.
If you understand how to make this script more efficient, the improved version will be property of you and can be used freely.
For the rest of you:
Script might be used if you give me "credit" in the staff roll, etc.. If you want to use this in a commercial game, PM me. This script is NOT to be used in any games that are entries for Yoyogames' competition06.
Hope you find this useful.
/* Script by Yal. Give credit if you don't clearly understand what the code does// just from reading it.//// Place in the END STEP event and an instance will now collide with tiles.// The code presumes a few things: FIRSTLY, the object needs the variable xspeed// (NOT hspeed but xspeed) for movement; set it to 0 in create event. Also set the// variable onground to 0. SECONDLY, the game MUST BE A PLATFORMER with gravity// being downwards. THIRDLY, the object who use this code must have its x origin// in the center of the sprite. Useful in platformers since you can use xscale// to flip the sprite instead of having one left and one right sprite. FINALLY;// all tiles in the layer you use must be squares - tiles are treated as squares// no matter what you do - so uphills are currently not supported. Oh, and the// layer you use for the collision tiles MUST ONLY CONTAIN tiles that are supposed// to be 'solid'. Have decoration tiles in other layers!//// Play around with constants to match your game.//// Benefits: you don't need wall objects anymore, saving you time when tiling// rooms. As well, your game will run faster since the wall instances took some// processing time to handle as well. More useful on slow machines (like mine).// but might be good on new ones as well, as long as you don't have too many// instances using this code.//*/if(xspeed == 0){xscale = image_xscale}else{xscale = sign(xspeed)}////Look in vertical axis//if(vspeed != 0){ colpos = tile_layer_find(1000000,x,y + vspeed + sprite_height - sprite_yoffset - (vspeed < 0)*sprite_height) ripos = tile_layer_find(1000000,x + sprite_width*0.5,y + vspeed + sprite_height - sprite_yoffset - (vspeed < 0)*sprite_height) lepos = tile_layer_find(1000000,x - sprite_width*0.5,y + vspeed + sprite_height - sprite_yoffset - (vspeed < 0)*sprite_height)}else{ colpos = tile_layer_find(1000000,x,y + sprite_height - sprite_yoffset + 1) ripos = tile_layer_find(1000000,x + sprite_width*0.5,y + sprite_height - sprite_yoffset + 1) lepos = tile_layer_find(1000000,x - sprite_width*0.5,y + sprite_height - sprite_yoffset + 1)}if(tile_exists(colpos) || tile_exists(ripos) || tile_exists(lepos)){ y = round(y) onground = true if(vspeed > 0){ y -= 1 } else if(vspeed < 0){ y += 1 } vspeed = 0}else{ vspeed = min(8,vspeed + 0.25) onground = false}////Look in horizonial axis//ripos = tile_layer_find(1000000,x + xspeed + xscale*sprite_width*0.5,y + sprite_height - sprite_yoffset - 1)colpos = tile_layer_find(1000000,x + xspeed,y + sprite_height - sprite_yoffset - 1) if(tile_exists(ripos) || tile_exists(colpos)){ xspeed = xspeed*0.5 } else{ x += xspeed }if(abs(xspeed) < 0.2){xspeed = 0}
Edited by Yal, 20 May 2010 - 07:41 AM.











