Jump to content

Arcade body.velocity.y returns NaN when extending Sprite


PabloK
 Share

Recommended Posts

Hi! I'm new to Phaser and have had great fun learning the engine. I recently started separating the code for my game into prefab files by extending Phaser.Sprite but I have run in to trouble. When Trying create the movement for the player I have a problem where the player starts moving randomly up or down on the y-scale. When inspecting my Player.prototype.update method in chrome I can see that this.body.velocity.y is NaN. I have no Idea how it got to be NaN pheraps some issue with the gravity? I have tried various fixes but I can't find the bug in my code. I have looked at numerous tutorials and they all look similar to what Im doing. Please help. Below is my code.

player.js

// Private
var leftKey;
var rightKey;
var jumpKey;
var speed = 150;
var jumpSpeed = 250;
// Constructor
function Player (game, x, y) {
  Phaser.Sprite.call(this, game, x, y, Player.key);
  this.anchor.set(0.5,0.5);
  game.physics.arcade.enableBody(this);
  this.body.bounce = 0;
  this.body.collideWorldBounds = true;
  this.body.allowGravity = true;

  leftKey = game.input.keyboard.addKey(Phaser.Keyboard.A);
  rightKey = game.input.keyboard.addKey(Phaser.Keyboard.D);
  jumpKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

  game.add.existing(this);
}
Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;

// Static
Player.preload = function(game) {
  game.load.image(Player.key, "images/block.png");
};
Player.key = "Player";

Player.prototype.update = function() {
  this.body.velocity.x = 0;

  if (leftKey.isDown) {
    this.body.velocity.x = -speed;
  } else if (rightKey.isDown) {
    this.body.velocity.x = speed;
  }

  if (jumpKey.isDown && (this.body.onFloor() || this.body.touching.down)) {
    this.body.velocity.y = -jumpSpeed;
  }
};

module.exports = Player;
var Player = require('../models/player.js');
var destiny = {};

/* The main game */
destiny.create = function () {
  console.info("Find out your destiny!");
  // World
  this.game.stage.backgroundColor = "#5599CC";
  this.game.world.setBounds(0, 0, 1000, 500);
  this.game.physics.startSystem(Phaser.Physics.ARCADE);
  this.game.time.desiredFPS = 30;
  this.game.physics.arcade.gravity.y = 200;
  this.allSprites = {};

  // Player
  this.allSprites.destinyPlayer = new Player(this.game,2000,900);


  // Camera
  this.game.camera.follow(this.allSprites.destinyPlayer,Phaser.Camera.FOLLOW_PLATFORMER);

};

destiny.update = function() {
};

destiny.render = function() {
  this.game.debug.spriteInfo(this.allSprites.destinyPlayer,32,32);
  this.game.debug.cameraInfo(this.game.camera,32,128);
};

module.exports = destiny;

My game is loaded in a series of game states (boot, preload, startscreen, preload, game). I have previously not had any problems with that part but if you want to see that code please ask.

Here is the error in chrome.

NAN.thumb.png.f4a0c3c5f3841ec358ec9eba64

Any help is appreciated.

Link to comment
Share on other sites

I have tracked the setting of the NaN value to the preUpdate checkWorldBounds function. Here the following code exists in phaser 2.4.6

            pos.y = bounds.bottom - this.height;
            this.velocity.y *= -this.bounce.y;
            this.blocked.down = true;

It seems this.bounce.y has not been initiated to 0 but is undefined. I'm thinking that this is a bug? Any one else had this problem?

Link to comment
Share on other sites

On second thought I think the problem was me setting the sprites bounce value to 0 when initiating. I found this in some tutorial. When setting it to 0 that removes the point that was previously set and the x and y values are destroyed. I think a nan value here should cause an exception if possible so that its easier to debug this fault.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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