Jump to content

Issue with timer


Recommended Posts

I have discovered a rather annoying problem.  My game is a platformer game with a large world.  I spawn robots based on map data.  Some robots will be set to fire at player.  Now the problem that I have is that a robot will fire when it comes into the camera view (camera is following player), but if that same robot goes out of the camera view and then I move back towards it and it comes into the camera view again, it doesn't fire.  Here is the method that I call.


fireTimer is set when the robot sprite is spawned.  It appears that the timer is possibly being cleared when the sprite moves outside of the cameras view.


Any help much appreciated


Robot.prototype.fire = function(){
  //if (!this.alive) { return; }
  if (Math.abs(this.x - this.state.player.x) > this.xDistanceFromPlayer){ return; }
  this.shots.fire(this.x, this.y, this.fireSpeed);
  this.fireTimer = this.game.time.events.add(this.game.rnd.integerInRange(1000,2000), this.fire, this);
Link to comment
Share on other sites

Once you get far away, the "return's" will stop the code and this:

this.fireTimer = this.game.time.events.add(this.game.rnd.integerInRange(1000,2000), this.fire, this);

will never run again. Reconstruct your code and avoid"return;"

Robot.prototype.fire = function(){  if (this.alive) {     if (Math.abs(this.x - this.state.player.x) < this.xDistanceFromPlayer) {        this.shots.fire(this.x, this.y, this.fireSpeed);     }  }  // now "this.fireTimer" will always run  this.fireTimer = this.game.time.events.add(this.game.rnd.integerInRange(1000,2000), this.fire, this);};
Link to comment
Share on other sites

Yep so i did the modifications


I have debugged the fireTimer using console.log and I can see the timer triggering and then added again ... but once the sprite is out of the camera view

and then move around until it is back in the camera view the timer doesn't seem to be doing anything, it is still set 


c.TimerEvent {timer: c.Timer, delay: 1276, tick: 1449564875317, repeatCount: -1, loop: false…}


Is this a problem in phaser?

Link to comment
Share on other sites

instead of

if (Math.abs(this.x - this.state.player.x) < this.xDistanceFromPlayer) {}


var detectionRange = 500; // or half the camera distanceif (game.math.distance(this.x, this.y, this.state.player.x, this.state.player.y) < detectionRange) {}

should work 100% and you can modify the detectionRange for other robot types

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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