mcuz Posted December 7, 2015 Share Posted December 7, 2015 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 More sharing options...
liakos1992 Posted December 8, 2015 Share Posted December 8, 2015 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 More sharing options...
mcuz Posted December 8, 2015 Author Share Posted December 8, 2015 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 viewand 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 More sharing options...
Skeptron Posted December 8, 2015 Share Posted December 8, 2015 Also, you could avoid doing this maths to calculate if the robot is in camera view and instead use sprite.inCamera Doc : http://phaser.io/docs/2.4.4/Phaser.Sprite.html#inCamera Link to comment Share on other sites More sharing options...
liakos1992 Posted December 8, 2015 Share Posted December 8, 2015 instead ofif (Math.abs(this.x - this.state.player.x) < this.xDistanceFromPlayer) {}dovar 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 More sharing options...
Recommended Posts