Ping Pong - Pause and Scrolling Text
| Category | : Flash | Views | : 26366 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : ping-pong-pause-scrolling-text.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series | Next >>> |
Now we need to modify our previous code so it runs only when the game is not paused, and vice versa. Add this code to instance of "Player" and "Ball". You will need to open "Actions" window panel and edit the code associated with the instance:
onClipEvent(enterFrame)
{
if(!_root.gpause)
{
//rest of the code we wrote before
.
.
.
}
}
Code Explanation
var gpause = false;
var keyHold = false;
The first variable tells whether the game is paused or not and the second variable whether the pause key is currently being pressed.
this.onEnterFrame = function()
{
if ((Key.isDown(80)) && (!keyHold))
{
gpause = !gpause;
If the "p" key just got pressed (80 in ascii) and it's not holding down we set the variable "gpause" to its contrary.So if gpause was false it will be true now.
To determinate whether a key is down or not use the following function:
Key.isDown(Key Code);
The 'Key code' is ascii, you can get these codes at http://www.asciitable.com
if(gpause)
{
_root.attachMovie("Scroll", "Scroll", 10, {_x: 100, _y: 150});
}
If the game is just got paused we attach the "Scroll" Movie Clip using the values 100 and 150 for the _x and _y properties
else
{
_root.Scroll.removeMovieClip();
}
If the game is not paused anymore, remove the "Scroll" Movie Clip.
keyHold = true;
The "p" key is down so set this variable to true until the player releases the key.
}
else if (!Key.isDown(80))
{
keyHold = false;
}
}
}
If the player releases the "p" key set this variable to false.
onClipEvent(enterFrame)
{
if(!_root.gpause)
{
//rest of the code we wrote before
.
.
.
}
}
The purpose of this code is to prevent the previous code that we have written to be run when the game is paused. The code will run only if the game is not paused.In the last tutorial of this series we are going to add sound and a simple menu at the beginning of the game.
More cool tech articles from other blogs.
| <<< Previous | Index Series | Next >>> |
