Jump to content

Player Health bar - error


xronn
 Share

Recommended Posts

So I'm trying to display a players health this is what I've come up with so far - 

Create -

healthbar = this.add.sprite(38, 616, 'healthbar');healthbar.fixedToCamera = true;healthbar.cropEnabled = true;maxHealth = 100;player.alive = true;

Update - 

healthbar.crop.width = (player.health / maxHealth) * healthbar.width;

Basically when a player takes damage the player.health will drop below the maxHealth which would hopefully crop the health bar, unfortunately I don't get any errors and when I take damage say down to 67 hp the crop doesn't run on the healthbar and it remains are full width.

Link to comment
Share on other sites

Hi, I believe, that you have to call updateCrop() after changing width in update method. This is part of my game code:

 

create:

            // top            this._top = new Sprite(aGame, 0, 0, "UI", "LimitTimeTop");            var topFrame = aGame.cache.getFrameByName("UI", "LimitTimeTop");            this._topHeight = topFrame.height;            this._topCropRect = new Phaser.Rectangle(0, 0, topFrame.width, topFrame.height);            this._top.crop(this._topCropRect, false);

sets new crop rect as big as original sprite's frame.

 

update:

            // top            var newHeight = Math.floor(this._topHeight * topPercent);            this._topCropRect.y = this._topHeight - newHeight;            this._topCropRect.height = newHeight;            this._top.updateCrop();            this._top.y = this._topHeight - newHeight;

chnges and updates cropping.

 

Anyway, if your health bar is colored rectangle bar, then it may be better and easier for you just set sprite's width to needed number of pixels.

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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