Sign in to follow this  
WholemealDrop

Reset game time when game state changes

Recommended Posts

I have finally gotten my game to a spot I'm very happy with after refactoring my code to use groups rather than individual instances of enemies. Once bug I found once doing this was that when the GameOver state is called, all 4 of my groups deploy an enemy at once rather than on a time delay like when you first load the game. I tried using time reset and removeAll from Time.js but kept getting errors due to things not being defined from the Time.js code. Any help would be appreciated.

 

Here is where I am using time

if (this.nextDustAt < this.time.now && this.dustPool.countDead() > 0){
        this.nextDustAt = this.time.now + this.dustDelay;
        var dust = this.dustPool.getFirstExists(false);
        dust.reset((this.game.width-100), Math.floor(Math.random() * this.game.height));
        dust.body.velocity.setTo(this.game.rnd.integerInRange(150,400)*-1), this.game.rnd.integerInRange(150,400);
    }

    if (this.nextDust2At < this.time.now && this.dustPool2.countDead() > 0){
        this.nextDust2At = this.time.now + this.dustDelay2;
        var dust2 = this.dustPool2.getFirstExists(false);
        dust2.reset((this.game.width-100), Math.floor(Math.random() * this.game.height));
        dust2.body.velocity.setTo(this.game.rnd.integerInRange(250,500)*-1), this.game.rnd.integerInRange(250,500);
    }

    if (this.nextDust3At < this.time.now && this.dustPool3.countDead() > 0){
        this.nextDust3At = this.time.now + this.dustDelay3;
        var dust3 = this.dustPool3.getFirstExists(false);
        dust3.reset((this.game.width-100), Math.floor(Math.random() * this.game.height));
        dust3.body.velocity.setTo(this.game.rnd.integerInRange(350,600)*-1), this.game.rnd.integerInRange(350,600);
    }

    if (this.nextDust4At < this.time.now && this.dustPool4.countDead() > 0){
        this.nextDust4At = this.time.now + this.dustDelay4;
        var dust4 = this.dustPool4.getFirstExists(false);
        dust4.reset((this.game.width-100), Math.floor(Math.random() * this.game.height));
        dust4.body.velocity.setTo(this.game.rnd.integerInRange(450,700)*-1), this.game.rnd.integerInRange(450,700);
    }

And here is where I am calling GameOver

//Check if lives = 0, run game over or new dust
livesCheck: function(){
    if (this.lives === 0)
    {
        this.gameOver();
    }
},

//game over, restart
gameOver: function() {    

    //pass it the score as a parameter
    this.game.state.start('GameOver', true, false, this.score);
},

 

Share this post


Link to post
Share on other sites

Nothing in the code you linked changes the value of any of the  nextDust[1-4]At values, so my guess is that you need to re initiate them in gameover state, which you didn't link either.

 

I would rather use timed events for stuff like these.

http://phaser.io/examples/v2/time/basic-timed-event

 

You also need to consider looking into Object Oriented Programming as your code looks bad. It will be harder for you to change it down the road, and you will eventually have to rewrite everything from scratch.

 

 

PS. I tried writing a small part on how to convert it into OOP however your arrangements are very wierd, and I failed at it.

 

var dust = {
  nextDustAt : [],
  dustPool : [{
    countDead : function(){
      //countDead function code goes here
    },
    anotherParamFORdustPool : anothervalueFORdustPool
  }],
  dustDelay : []
};


for(counter = 0; counter < 4; counter++){
  if (this.dust.nextDustAt[counter] < this.time.now && this.dust.dustPool[counter].countDead() > 0){
        this.dust.nextDustAt[counter] = this.time.now + this.dust.dustDelay[counter];
        
        
        /*
        var dust4 = this.dustPool4.getFirstExists(false);
        dust4.reset((this.game.width-100), Math.floor(Math.random() * this.game.height));
        dust4.body.velocity.setTo(this.game.rnd.integerInRange(450,700)*-1), this.game.rnd.integerInRange(450,700);
        */
    }
}

PPS.

Seriously look into OOP Javascript you will end up writing better code that is easier to modify and debug.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

Share this post


Link to post
Share on other sites

I added this code based on an example and then extended it for additional enemies so that's why I didn't use a timed event because the exampled didn't use one. And sorry, nextDust[1-4]At is defined at each group as an int in MS of time. So after an allotted time, a new dust should be created. If this is unclear, here is my git repo of it: DustPong2.0

I honestly didn't think my code was that bad looking (my manager said it looked great!) I know it isn't perfect since it has been years since I did JavaScript and this is my first Phaser project. I do appreciate the feedback though! 

Share this post


Link to post
Share on other sites

The reason i'm saying it's weird is because parts such as these

var dustParticle;
var dustParticle2;
var dustParticle3;
var dustParticle4;

Which can be written as 

var dustParticle = [];

Making it into an Array with as many dust particles as you need, and access them with dustparticle[0], dustparticle[1]. This in turn helps with coditionals where you need to iterate through all the instances like in your code written above.

 "And sorry, nextDust[1-4]At is defined at each group as an int in MS of time. "

This is what I was talking about in my first post. When you mention "each group" that means an array of groups, an array that can have all it's members be objects which in turn can have other arrays in them.

Now back on topic.

You have timers but you need to read how timer works. There are 2 major edits that you need to do

//Check if lives = 0, run game over or new dust
livesCheck: function(){
    if (this.lives === 0)
    {
    	this.world.removeAll();
        this.gameOver();
    }
},

world.removeAll() will clear all your game state stuff.

    this.nextDustAt = 0+this.time.now;
    this.dustDelay = 2000+this.time.now;
[...]
    this.nextDust2At = 8000+this.time.now;
    this.dustDelay2 = 7000+this.time.now;

[...]
    this.nextDust3At = 14500+this.time.now;
    this.dustDelay3 = 14500+this.time.now;

[...]
    this.nextDust4At = 19000+this.time.now;
    this.dustDelay4 = 19000+this.time.now;

You need to time all your events on current time, Those are the ones I noticed but there could be more.

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.