# Adding a pause game function

**URL:** <https://forum.kirupa.com/t/adding-a-pause-game-function/242559>\
**Category:** programming\
**Created:** [October 27, 2007, 2:38pm UTC](https://forum.kirupa.com/t/adding-a-pause-game-function/242559 "2007-10-27T14:38:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose3760](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jose3760/32/2887_2.png) [@jose3760](https://forum.kirupa.com/u/jose3760)\
**Post date:** [October 27, 2007, 2:38pm UTC](https://forum.kirupa.com/t/adding-a-pause-game-function/242559/1 "2007-10-27T14:38:34Z")

</div>

Hey guys! well, basically the title says it all. I need help in creating a pause game button in my snake game. Ive attempted it myself, but it was just an epic fail.

heres the code on the frame:

> stop();  
> \_root.options.\_visible = false;  
> //Variables and peice width and height  
> var unit = 15;  
> var uwh = 20;  
> var canMove = false;  
> var dir = 2;  
> var score = 0;

//add the keydown listener for when you press space to start  
//addpeicelist stores how long your tail is  
aPieceList = new Array();  
keydown = new Object();

//If the space key is pressed, start the game  
keydown.onKeyDown = function(){  
if(!canMove) {  
if(Key.isDown(Key.SPACE)){  
canMove = true;  
startGame();  
}  
}  
}  
key.addListener(keydown);

//this gets the arrow keys when their pressed, and moves your snake accordingly  
k = new Object();  
k.onKeyDown = function() {  
var k = Key.getCode();  
if (k == Key.UP && dir != 2 && canMove) {  
dir = 0;  
canMove = false;  
} else if (k == Key.LEFT && dir != 3 && canMove) {  
dir = 1;  
canMove = false;  
} else if (k == Key.DOWN && dir != 0 && canMove) {  
dir = 2;  
canMove = false;  
} else if (k == Key.RIGHT && dir != 1 && canMove) {  
dir = 3;  
canMove = false;  
}  
};

//This adds a peice to the end of your character  
Key.addListener(k);  
function addPiece() {  
var p = this.attachMovie(“piece”, “piece”+aPieceList.length, aPieceList.length);  
p.\_x = aPieceList[aPieceList.length-1].\_x;  
p.\_y = aPieceList[aPieceList.length-1].\_y;  
aPieceList.push(p);  
}

//This spawns the food peices in different locations  
function moveFood() {  
var moveIt = true;  
while (moveIt) {  
food.\_x = Math.floor(Math.random()\*uwh)\*unit;  
food.\_y = Math.floor(Math.random()_uwh)unit;  
moveIt = false;  
for (var i = 0; i\<aPieceList.length; i++) {  
if (aPieceList.\_x == food.\_x && aPieceList_.\_y == food.\_y) {  
moveIt = true;  
}  
}  
}  
}

//when you die, this ends the game and deletes everything on the stage  
function gameOver() {  
delete this.onEnterFrame;  
canMove = false;  
}

//This function starts th game, sets up your snake, the score, etc  
function startGame() {  
for (var i = aPieceList.length-1; i\>=0; i–) {  
aPieceList\*.removeMovieClip();  
aPieceList.pop();  
}  
score = 0;  
var p = this.attachMovie(“piece”, “piece”+aPieceList.length, aPieceList.length);  
aPieceList.push(p);  
p.\_x = 10_unit;  
p.\_y = 10_unit;  
var food = this.attachMovie(“food”, “food”, -1);  
var c = 0;  
moveFood();  
var startingLength = 3;  
for (var i = 1; i\<startingLength; i++) {  
addPiece();  
}  
this.onEnterFrame = function() {  
canMove = true;  
tScore.text = score;  
for (var i = aPieceList.length-1; i\>0; i–) {  
aPieceList\*.\_x = aPieceList[i-1].\_x;  
aPieceList\*.\_y = aPieceList[i-1].\_y;  
}  
if (dir == 0) {  
aPieceList[0].\_y -= unit;  
} else if (dir == 1) {  
aPieceList[0].\_x -= unit;  
} else if (dir == 2) {  
aPieceList[0].\_y += unit;  
} else if (dir == 3) {  
aPieceList[0].\_x += unit;  
}  
if (aPieceList[0].\_y/unit == 20) {  
aPieceList[0].\_y = 0;  
} else if (aPieceList[0].\_y/unit == -1) {  
aPieceList[0].\_y = 19_unit;  
} else if (aPieceList[0].\_x/unit == -1) {  
aPieceList[0].\_x = 19_unit;  
} else if (aPieceList[0].\_x/unit == 20) {  
aPieceList[0].\_x = 0;  
}  
if (aPieceList[0].\_x == food.\_x && aPieceList[0].\_y == food.\_y) {  
score += 1;  
moveFood();  
addPiece();  
}  
for (var i = 1; i\<aPieceList.length; i++) {  
if (aPieceList[0].\_x == aPieceList\*.\_x && aPieceList[0].\_y == aPieceList\*.\_y) {  
gameOver();  
}  
}  
};  
}

//This is my attemot at making the game pause. it is an epic phail.  
function pausegame (){  
canMove = false;  
};

and heres the code on the botton:

> on(press){  
> pausegame ();  
> }

Thanks for your time,  
Joseph
