Jump to content

Issue adding animation to extended sprite


LTNGames
 Share

Recommended Posts

I'm recieving an error when trying to add an animation to my sprite., the log points to this in phaser

//  Set-up some event listeners
    this.game.onPause.add(this.onPause, this);

I've been trying to figure this out for an hour now and I keep getting issues, I figured it has to be the way I'm instantiating the sprite or the way I'm loading the atlas, either way I'm stuck and would appreciate any help you can offer me.

These are my functions

Game.State_Load = function() {
};

Game.State_Load .prototype = Object.create(Game.State_Base.prototype);
Game.State_Load .prototype.constructor = Game.State_Load;

Game.State_Load .prototype.preload = function() {
  this.load.atlasJSONHash('player', 'img/characters/playerCat/walk.png', 'img/characters/playerCat/walk.json');
};

Game.State_Load .prototype.create = function(){
  Game.StateManager.goto("Level");
};

//----------------------------------------------------------
// Main level
//----------------------------------------------------------
Game.State_Level = function() {
};

Game.State_Level.prototype = Object.create(Game.State_Base.prototype);
Game.State_Level.prototype.constructor = Game.State_Level;

Game.State_Level.prototype.init = function() {
  this.startPhysics();
};

Game.State_Level.prototype.startPhysics = function(){
  this.game.physics.startSystem(Phaser.Physics.ARCADE);
  this.game.physics.arcade.gravity.y = 1000;
};

// Extended sprite!
Game.State_Level.prototype.create = function(){
  this.player = new Game.Sprite_Player(this, this.world.centerX, this.world.centerY, "player");
};

Game.State_Level.prototype.update = function(){

};

This below is the extended sprite, which works fine when I don't try and add an animtion.

Game.Sprite_Player = function() {
  this.init.apply(this, arguments);
};

Game.Sprite_Player.prototype = Object.create(Phaser.Sprite.prototype);
Game.Sprite_Player.prototype.constructor = Game.Sprite_Player;

Game.Sprite_Player.prototype.init = function(game, x, y, key, frame) {
  Phaser.Sprite.call(this, game, x, y, key, frame);
  game.add.existing(this);
  //Physics--------
  game.physics.arcade.enable(this);
  this.body.collideWorldBounds = true;
  //Refrence
  this.cursor = game.input.keyboard.createCursorKeys();

  this.animations.add('walk');
};

 

Link to comment
Share on other sites

Okay, I solved it, I swear it was the first thing I did when I first got this error. My problem was the invoking of the sprite as I thought in the first place, instead of calling this in the game argument for my extended sprite I had to call this.game. The first time I did it I must not have noticed  a new error or something and somewhere down the line, after adjusting and resetting almost everything I fixed it Anyway, I'm good to go now.

 

Just in case someone in the future has the same problem I'll add some code boxes to clarify.

// Not working.
this.player = new Game.Sprite_Player(this, this.world.centerX, this.world.centerY, "player");
// WORKING!                         
this.player = new Game.Sprite_Player(this.game, this.world.centerX, this.world.centerY, "player");

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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