Jump to content

How can I change the image of a sprite during update function?

Recommended Posts

I'm very new to Phaser, sorry if this is a dumb question.

I am working on a game where I want the health of the player to be expressed as pixelated heart. It should grey out if the player has lost it or should stay red if they have good health.

(Similar to minecraft's health system)

So, I loaded in both the sprites I made in Aseprite this way.

this.load.image('heart', 'Images/heart.png');
this.load.image('heart-grey', 'Images/heart-grey.png');

Then I create all 5 hearts this way.

var heart1 = this.add.sprite(0, 0, 'heart').setOrigin(0, 0);
var heart2 = this.add.sprite(32, 0, 'heart').setOrigin(0, 0);
var heart3 = this.add.sprite(64, 0, 'heart').setOrigin(0, 0);
var heart4 = this.add.sprite(96, 0, 'heart').setOrigin(0, 0);
var heart5 = this.add.sprite(128, 0, 'heart').setOrigin(0, 0);

The hearts display perfectly. I want to change the keyword(3rd argument) of heart1 during my update function. How do I go about doing this?

Edited by PyxelPranav
Making title more clear
Link to post
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...