Jump to content

Swap Characters


Recommended Posts

I'm trying to implement a system that allow the player to swap characters mid game. But I'm stuck.

The idea is just to press a key and swap the current character with the next. That's it. 

I'm not sure how to approach it. 

First I tried to just use an If statement and just changing the value of the character, but that didn't work.

The process was just  create a new character like 

this.character = new Character({
      game: this.game,
      x: this.world.centerX,
      y: this.world.centerY,
      asset: 'character1'



Then just every time a key was pressed (like A) call a function that would assign a new value to this.character.  But it throw a null error so I give up. 

I like the swap mechanic but I don't know how do I make it work for different characters.  

Do I have to put all the characters (except the first one) off screen and change positions that way? on screen move off screen, off screen move on screen? Does it even work like that?

is there already any implementation for this?

Any idea  or advice will be appreciated. 

Link to comment
Share on other sites

There are different ways to achieve this. You can keep some track of some counter, have A list of sprites and use loadTexture to swap trough the list of sprites. If you want more flexibility, here is an example using custom classes:

The idea is to extend phaser group A little bit by adding a swap function. The swap function then switches characters (not just the texture, the entire object). In the example the group is moved troughout the world, but you could just as well move each Character object individually (Character.prototype. update instead of Characters.prototype.update).

The example is adapted from A character selection snippet I made once:

You might be able to combine the two examples if you need some character selection screen/UI.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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