Jump to content

state and sprite bug


Recommended Posts


I modified one of the examples from the phaser tutorial to use states: playState -> nextLevelState -> playState -> ..

playState has the player and an enemy

nextLevelState shows the next level number and has a continue button

I use the init method to pass a level number.

Now when I start the playState for the second time some sprites are moving! ie. what happened playing level 1  is remembered when I start playstate with level 2 !

My enemy sprite moves even though I re-add the sprite in playState.create and my when i move the player I get:



Uncaught TypeError: Cannot read property 'velocity' of null
    at c.Physics.Arcade.moveToXY (phaser.min.js:24)
    at playState.update (game.js:303)
    at c.StateManager.update (phaser.min.js:10)


I use game.state.start('NextLevelState', true, false, current_level); //clearWorld=true

and  game.state.start('playState', true, false, current_level);

What am I missing regarding states and recreating sprites (game.add.sprite)?


Link to comment
Share on other sites

The problem was phasers crappy documentation.

When a state is started again you have make sure your member variables are reset properly in the create call.


i had a list (this.players = []) which only where appended t in create() so it would end up with extra elements. fix is to add this.players = [] in create(..)


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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