- Title: Classic Pause
- Description: A method to pause the game.
- GM Version: 7
- Registered: no
- File Type: .gmk
- File Size: 48k
- File Link: Classic Pause
What I'm showing here is a way to pause the game by "manually" deactivating the instances so that they don't disappear (like in old Konami NES games). This method is highly customizable, but every step must be followed carefully. This method is different than those showed here :YoYoGame Wiki
First of all, you must create a variable named 'pausable' for ALL your objects.
ALL OBJECTS: CREATE EVENT
pausable = 1; //0 = Can't be paused. 1 = Can be paused.
Then you put all the code for pausing the game in the begin step event of a controller object.
CONTROLLER OBJECT: CREATE EVENT
{
pausable = 0; //This object can't be paused.
globalvar background_music,pause; //Sets a global variable for the background music and the pause state.
pause = 0;
/*This will set and play the background music. You can also put this in the room creation code
if you want a different music to play for each level*/
background_music = sound0;
sound_volume(background_music,1);
sound_loop(background_music);
}CONTROLLER OBJECT: BEGIN STEP EVENT
{
// Pause Game
if ( !pause )
{
if ( keyboard_check_pressed(vk_enter) ) //Pauses the game when the player press Enter.
{
pause=1; //The game is paused.
sound_volume(background_music,0.85); //Reduces the volume of the background music (optional).
sound_play(snd_pause); //Plays a sound when the game is paused (optional).
instance_create(view_xview+view_wview/2,view_yview+view_hview/2,obj_pause); //Creates an object to display a pause sprite when the game is paused (optional).
with ( all ) //Saves the state of all "pausable" instances and put all their local variable to 0.
{
if ( pausable )
{
prev_image_speed=image_speed;
prev_vspeed=vspeed;
prev_hspeed=hspeed;
prev_gravity=gravity;
prev_path_speed=path_speed;
prev_timeline_speed=timeline_speed;
image_speed=0;
vspeed=0;
hspeed=0;
gravity=0;
path_speed=0;
timeline_speed=0;
}
}
}
}
else if ( keyboard_check_pressed(vk_enter) ) //Resume the game when the player press Enter.
{
pause=0; //The game resumes.
sound_volume(background_music,1); //Music volume is back to normal.
with (obj_pause) instance_destroy(); //Destroys the pause object.
with ( all ) //Restore the state of all instances.
{
if ( pausable )
{
image_speed=prev_image_speed;
vspeed=prev_vspeed;
hspeed=prev_hspeed;
gravity=prev_gravity;
path_speed=prev_path_speed;
timeline_speed=prev_timeline_speed;
}
}
}
if ( pause )
{
with ( all ) //Maintains all the alarms of "pausable" objects to the same value while the game is paused.
{
if ( pausable )
{
if (alarm[0]>0) alarm[0]+=1;
if (alarm[1]>0) alarm[1]+=1;
if (alarm[2]>0) alarm[2]+=1;
if (alarm[3]>0) alarm[3]+=1;
if (alarm[4]>0) alarm[4]+=1;
if (alarm[5]>0) alarm[5]+=1;
if (alarm[6]>0) alarm[6]+=1;
if (alarm[7]>0) alarm[7]+=1;
if (alarm[8]>0) alarm[8]+=1;
if (alarm[9]>0) alarm[9]+=1;
if (alarm[10]>0) alarm[10]+=1;
if (alarm[11]>0) alarm[11]+=1;
}
}
}
}Finally, you must put a little piece of code in certain events of all the "pausable" objects in order to tell them to stop performing certain events while the game is paused.ALL OBJECTS: STEP EVENTS, KEYBOARD/MOUSE EVENTS, COLLISION EVENTS
if ( pause ) exit; //Put this before any other code.
That's about it! I hope it helps.
Edited by pixeltao, 27 October 2012 - 09:01 PM.











