Search the Community
Showing results for tags 'timerevent'.
-
I'm creating a "arena" survival game, and have many AI tanks that follow and attack the player. I'm extending the Sprite object for all of my game "entities". I'm using TimerEvents fairly frequently to automatically update different aspects of each enemy. My problem is, once I spawn even a small number of enemies (20-30ish), the number of TimerEvents in the main game timer starts to rise since each tank has around 3 TimerEvents to itself. I've realized having a TimerEvent for every single aspect of the tank seems to be slowing down my game, but I can't see a better alternative. A couple partial examples of what I'm using them for (the "FREQ" delays are 100ms): move(direction: MoveDirection) { this.moveTimer = this.tankSprite.game.time.events.loop(TankMoveManager.CHECK_DIRECTION_FREQ, () => this.updateVelocity(direction), this); this.moveDirection = direction; } private updateVelocity(direction: MoveDirection) { if (direction === MoveDirection.NOT_MOVING) return; let angle = direction === MoveDirection.FORWARD ? this.tankSprite.angle : TankMoveManager.reverseAngle(this.tankSprite.angle); this.tankSprite.body.velocity = this.tankSprite.game.physics.arcade.velocityFromAngle(angle, this.moveVelocity); } This is a part of every tank Sprite, including the player. Since tanks can really only move forward, I'm having the tank move forward, and periodically adjusting the velocity to account for the angle that it's facing. startAttacking() { if (this.firingTimer != null) { return; } this.host.setFiringAngle(null); this.firingTimer = this.host.game.time.events.loop(AlignedShotManager.DIRECTION_UPDATE_FREQ, () => { let angleToTarget = Phaser.Math.radToDeg(this.host.game.physics.arcade .angleToXY(this.host, this.target.x, this.target.y)); if (Math.abs(this.host.angle - angleToTarget) < AlignedShotManager.TARGET_ANGLE_EPSILON) { this.host.startFiring(); } else { this.host.stopFiring(); } }, this); } This is in every enemy tank. It periodically checks if it's facing the target (the player), and starts firing if it is. startFiring() { if (!this.firingTimer) { let shotDelay = this.gunProperties.delayBetweenShots; this.fireBullet(); this.firingTimer = this.game.time.events.loop(shotDelay, () => this.fireBullet(), this); } } This is a part of the Gun class, which is also a part of every tank. I'm using the timer to continually fire bullets until it's told to stop firing. All of the above methods have a "stop" variant that removes the TimerEvent from the Timer, and handles any cleanup. After testing, I've made sure I'm not leaking TimedEvents. What can I do to prevent the number of TimerEvents from exploding and plugging up the Timer? I thought of having a single TimerEvent per tank, and just adding new tasks on by chaining the callback, but that prevents me from stopping a single task; I could only kill all the tasks in the callback or none at all (I couldn't stop firing but continue moving). Any guidance here would be greatly appreciated.
-
I want custom timer. I make a memory game i.e I have to choose from a set of cards and find all the pairs. So if I pick two non-equal cards my timer should run their closing animation. I did this: cardTimer = new Phaser.Timer(gameObject, false); cardClosingEvent = function () { // doo some stuff cardTimer.stop(false); } cardTimer.add(1000, cardClosingEvent); openCards =function () { // do some stuff cardTimer.start(); } if I log the timer object in console, the events subarray is empty. Not sure what it goes wrong. It is like the TimerEvent is removed from the timer after the timer runs once.
- 1 reply
-
- phaser 2.2.2
- timer
-
(and 1 more)
Tagged with: