Datsik
Datsik

Reputation: 14824

Phaser.js getting Invalid Phaser state object given

I'm currently trying to play around with Phaser 2d game engine.

I currently have this bit of code:

// Generated by CoffeeScript 1.10.0
(function() {
  var create, game, preload, update;

  game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
    preload: preload,
    create: create,
    update: update
  });

  preload = function() {
    return game.load.atlasJSONHash('seyan_f_torch', '../sprite_hashes/seyan_f_torch.png', '../sprite_hashes/seyan_f_torch.json');
  };

  create = function() {
    var seyan_f_torch;
    seyan_f_torch = game.add.sprite(0, 180, 'seyan_f_torch', '00219000.png');
    seyan_f_torch.animations.add('walk-down', Phaser.Animation.generateFrameNames('', 219016, 219023, 9), 10, true, false);
  };

  update = function() {};

}).call(this);

Then when I try to load it I get this:

Invalid Phaser State object given. Must contain at least a one of the required functions: preload, create, update or render phaser.js:34530

Phaser v2.4.6 | Pixi.js v2.2.9 | WebGL | WebAudio http://phaser.io ♥♥♥

Uncaught TypeError: Cannot set property 'game' of undefined phaser.js:29106

Not sure what's wrong here. Any information would be great thanks.

....

Here's the actual CS file:

game = new (Phaser.Game)(800, 600, Phaser.AUTO, '',
    preload: preload
    create: create
    update: update)
    
    
preload = ->
    game.load.atlasJSONHash 'seyan_f_torch', '../sprite_hashes/seyan_f_torch.png', '../sprite_hashes/seyan_f_torch.json' 

create = ->
    # Create Seyan_F_Torch
    seyan_f_torch = game.add.sprite(0, 180, 'seyan_f_torch', '00219000.png')
    seyan_f_torch.animations.add('walk-down', Phaser.Animation.generateFrameNames('', 219016, 219023, 9), 10, true, false);
    setTimeout ->


update = ->

Upvotes: 0

Views: 845

Answers (2)

Esshahn
Esshahn

Reputation: 430

I ran into the same problem and found out, that the function syntax is important for Phaser.

Instead of

preload = function() {
};

it needs to be

function preload() {
};

Upvotes: 0

PhotonStorm
PhotonStorm

Reputation: 3385

I don't CoffeeScript, but I have thought the problem is that you're not passing an Object to the Game constructor. Phaser requires a well formatted State Object to start from. You can find an example in the Phaser.State class (in the repo its in src/core/State.js). Likely all you need to do is this:

game = new (Phaser.Game)(800, 600, Phaser.AUTO, '',
    { preload: preload,
    create: create,
    update: update}
)

Upvotes: 2

Related Questions