+ 2
How to add a button in a game that on clicking save the progress of game and another button that pause the game and resume it ?
Take this game and add solution to it You will be very helpful to me https://code.sololearn.com/WVO0RKpLduTI/?ref=app Try this keep the button in right bottom of the canvas
6 odpowiedzi
+ 2
Anton Böhler I have tried a lot
+ 2
//thats in the JS
//alot of variables here
var paused = false;
function togglePause(){
paused = !paused;
}
//here is some code that I didn't change at all...
Game = function(){
this.create=function(){
enemyDieCount=0;
enemyCount = 20;
//Таймер создания врагов
timerCreateNewEnemys = new gbt.GBT_TimerOut();
//Таймер выстрела врага
timerShootEnemys = new gbt.GBT_TimerOut();
//Таймер анимации бонусного танка
timerAnimationBonusTank = new gbt.GBT_TimerOut();
timerShootPlayer = new gbt.GBT_TimerOut();
timerEnemyStopMove = new gbt.GBT_TimerOut();
timerProtectBase = new gbt.GBT_TimerOut();
timerBonusDraw = new gbt.GBT_TimerOut();
rightPanel = new RightPanel(resourse[0]);
map = new Levels(resourse[0],level);
eagle = new Eagle(resourse[0]);
bonus = new Bonus(resourse[0]);
//Наш игрок 5 параметров картинка и координаты анимации х у, и место размещения х у
player = new Tank(resourse[0],0,0,5*gamePanel.width/14,gamePanel.height+gamePanel.y-(gamePanel.height/13));
player.improved=0;
//Создем пока что одну пулю нашему танку
if(bullets.length==0){
bullet = new Bullet(resourse[0],player.getX()+player.getWidth()/4,player.getY());
bullets.push(bullet);
}
//И по одной для врагов
createEnemyTank();
for(var i=0; i<enemys.length;i++){
if(enemys[i].bulletCount==0 && enemyBullets.length <4){
bullet = new Bullet(resourse[0],enemys[i].getX()+enemys[i].getWidth()/4,enemys[i].getY());
bullet.isEnemy = enemys[i];
enemyBullets.push(bullet);
}
}
createGameOver(resourse[0]);
//Добавляем первого врага на сцену
addEnemy();
for(var i=0; i<enemyBullets.length; i++){
enemyBullets[i].aliveBullet=false;
}
for(var i=0; i<bullets.length; i++){
bullets[i].aliveBullet=false;
}
}
this.update=function(){
if(paused){
return;
}
//rest of script
+ 2
and in html (in body):
<button onclick="togglePause()">pause</button>
+ 1
if your doing this on your pc only for you, you could probably save the progress in something like a textdocument otherwise you will need a database or something like that ...
For a pause button you would just have a boolean variable (true/false) which gets toggled by a button.
And before you update the screen you would check if the variable is true, to see if you need to update it or if it is paused
+ 1
thats for pause, the other I can't do ...
0
you should try it yourself first, otherwise you won't improve your skills ...
happy coding!