Jump to content

How can I reset my game?


Geroy290
 Share

Recommended Posts

I haven't figured out how I can make this function reset some of my variables, can anyone help me out with my "restartGame()" function? I want to reset my animals as well.  I just started using phaser today, so as you can see I am having a bit of a struggle...Anyway, here is my code:

var game = new Phaser.Game(600, 400, Phaser.AUTO, 'gameDiv', {preload: preload, create: create, update: update});function preload() {    game.stage.backgroundColor = 'FFFFFF';    game.load.image('player', 'assets/player.png');    game.load.image('animal', 'assets/animal.png');};var player;var jumpTimer = 0;var jumpButton;var animals;var timer;var score;var scoreLabel;var firstTimer;var timer2;function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);        player = game.add.sprite(100, 100, 'player');    game.physics.arcade.enable(player);    player.body.gravity.y = 700;    animals = game.add.group();    animals.enableBody = true;     animals.createMultiple(20, 'animal');    game.physics.enable(player, Phaser.Physics.ARCADE);    player.body.collideWorldBounds = true;    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    timer = game.time.events.loop(1500, addAnimals, this);    firstTimer= game.time.events.add(Phaser.Timer.SECOND * 3.85, firstRun, this);    score = 0;    scoreLabel = game.add.text(100, 100, "0", { font: "30px Arial", fill: "FFFFFF" });        }function update() {     player.body.velocity.x = 0;    if(jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer){        player.body.velocity.y = -250;        jumpTimer = game.time.now + 750;    };    game.physics.arcade.overlap(player, animals, restartGame, null, this);  }function firstRun() {    score += 1;    scoreLabel.text = score;    timer2 = game.time.events.loop(1500, increaseScore, this);}function increaseScore() {    score += 1;    scoreLabel.text = score;}function addOneAnimal(x, y){    var animal = animals.getFirstDead();    animal.reset(x, y);    animal.body.velocity.x = -200;    animal.checkWorldBounds = true;    animal.outOfBoundsKill = true;}function addAnimals() {    var hole = Math.floor(Math.random() * 5) + 1;    for (var i = 0; i < 1; i++)        if(i != hole && i != hole +1)            addOneAnimal(600, 375);}function restartGame(){    player.reset(100, 100)    score = -1;    }

Thanks for any help!

Link to comment
Share on other sites

For the animals, add

animals.forEachAlive(function(animal) {       animal.kill();} , this);

this will kill all sprites in the animals group and they will be able to get used again.

 

 

What problems do you encounter when you call the restartGame function?

 

Can you first explain how  do you want the game to flow? Then, we can point out what you do wrong.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...