Platformer
increase room width:
create the sprites, objects & room. Set objBlock property: Solid
add actions as follows, note Key Down - Right is the same as left, just positive value (not negative)
enable Viewport 0 in room properties, adjust settings as you please:
set up scene same as above, make sure objBlock property: Solid
objDude Step Event:
/// @description player controller //controls for movement: hkey = keyboard_check( vk_right ) - keyboard_check( vk_left ); //check if on ground: if( place_free( x, y+1 )){ gravity = 0.8; //set gravity - we are in the air! } else { gravity = 0; //no need for gravity on ground //since we are on ground at this point, we can check if we need to jump: if( keyboard_check_pressed( vk_up )){ vspeed = -12; } } //set horizontal movement based on controls: if( hkey == 0 ){ hspeed *= 0.75; //friction if( abs( hspeed ) < 0.5 ){ hspeed = 0; } } else { hspeed *= 0.75; hspeed += 2 * sign( hkey ); }
objDude End Step Event:
/// @description collision correction! //First check if we need to correct for collision: if( speed != 0 and x == xprevious and y == yprevious ){ //move up first if vspeed is up: if( vspeed < 0 ){ move_contact_solid( 90, -vspeed ); } //move over if hspeed is not 0: if( hspeed != 0 ){ //(setting some temporary variables we will use): temp_y = y; //move up: move_contact_solid( 90, abs( hspeed )); //move over: move_contact_solid( 90 - sign( hspeed ) * 90, abs( hspeed )); //move down (what we've moved up): if(temp_y-y != 0){ move_contact_solid( 270, temp_y-y); } //move down again if it puts us on ground: if( !place_free( x, y + abs( hspeed ) + 1 )){ move_contact_solid( 270, abs( hspeed )); } } //move down if vspeed is down: if( vspeed > 0 ){ move_contact_solid( 270, vspeed ); } //if we are blocked then set vspeed to 0: if( !place_free( x, y + sign( vspeed ))){ vspeed = 0; } //set hspeed to 0 if we can't move horizontally (we're blocked): if( hspeed != 0 and x == xprevious ){ hspeed = 0; } }
objDude Collision Event (with objBlock):
///empty script to trigger collision :)