Jump to content

Reset(x, y) function does not get x and y coordinates


frokenstein
 Share

Recommended Posts

Sorry to post here again, but I ran into another weird issue. My function for creating an alien sprite is as follows:

createAlien: function() {this.alien = this.aliens.getFirstDead();var start = this.game.rnd.integerInRange(1, 4);switch (start) {case 1:var x = this.game.world.bounds.width;var y = this.game.rnd.integerInRange(0, this.game.world.bounds.height);break;case 2:var x = 0;var y = this.game.rnd.integerInRange(0, this.game.world.bounds.height);break;case 3:var x = this.game.rnd.integerInRange(0, this.game.world.bounds.width);var y = this.game.world.bounds.height;break;case 4:var x = this.game.rnd.integerInRange(0, this.game.world.bounds.width);var y = 0;break;}if (!this.alien) {this.alien = new Alien(this.game, x, y, this.MAXCHARGE, this.MAXHEALTH, this.alienScale);this.aliens.add(this.alien);}this.alien.reset(x, y);this.alien.revive();}

As you can see, I am generating the x, y for a new and a revived alien. When I create the first new alien, it works fine, but after that alien dies and I revive the sprite, both x and y show up as NaN. I am trying to figure out why but I am at a loss. What did I do wrong? Here is my alien class:

var Alien = function(game, x, y, charge, health, scale, key, frame){key = 'alien';Phaser.Sprite.call(this, game, x, y, key, frame);this.anchor.setTo(0.5);this.scale.setTo(scale);this.maxcharge = charge;this.maxhealth = health;this.charge = this.maxcharge;this.health = this.maxhealth;this.alive = true;game.physics.arcade.enableBody(this);this.body.collideWorldBounds = false;this.body.bounce.set(1);this.events.onRevived.add(this.onRevived, this);};Alien.prototype = Object.create(Phaser.Sprite.prototype);Alien.prototype.constructor = Alien;Alien.prototype.onRevived = function() {this.charge = this.maxcharge;this.health = this.maxhealth;this.alive = true;};

Isn't reset() supposed to take an x and y coordinate and plug it into the object? When I console log the alien position I always get NaN for both coordinates for the revived alien. The first alien to be constructed works fine.

Link to comment
Share on other sites

Two places. First at the beginning of the game. This creates an alien that works.

 

...and in the update() function. If an alien was killed, after a timer event a new one is created here:

if (this.alienTimer < this.game.time.now) {this.alienTimer = this.game.time.now + this.alienInterval;if (this.aliens.countLiving() < this.TOTALALIENS) {this.createAlien();}}

I have verified the second alien is created, but it has NaN for rotation, x, y, etc. But its health is back to 100, its alive is true, and the other indications if it being alive look correct. It just never appears on the screen. It seems to be some kind of ghost object but I cannot see what is missing.

Link to comment
Share on other sites

Odd that it's coming up as NaN - numeric values should typically either be numeric or undefined; NaN suggests somewhere the values are being incorrectly set by trying to calculate with an undefined or non-numeric value in the calculation. Would it be possible to create a test case for this and upload it? Ideally something on jsfiddle or Codepen where we may be allowed to tinker with it?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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