clamp

0 favourites
  • 7 posts
  • Hi all,

    I am making a Powerbar for my game, and I followed Kiyoshi's tutorial:

    Make a Fancy Lifebar

    I get it to work, except if the value of the bar goes into a negative number, then the bar runs outside it's container, if that makes sense?

    On communicating with Kiyoshi, he mentioned the clamp function to constrain the values to within the parameters of the bar, and although I understand the concept, I do not have a clue in how to implement it.

    Can anyone help?

    Karl

  • clamp(value,minimum,maximum)

  • Hi Yann,

    As I said, I understand the concept of the clamp function, I have no idea how to implement it.

    My .capx should be here, I have never used Dropbox before, I have just set it up so I can supply my .capx

    https://www.dropbox.com/s/lly5ck2zuglkiwq/HUDDemo.capx

    If that doesn't work let me know and I will try to figure it out.

    So, what you are looking at are actually four bars, the blue one on top is your Power bar, the little orange one is your Grenade bar, the red one to the left of that is your Bullet bar and the box in the top left corner is your Health bar. The health bar works differently from the others, as it goes upwards instead of across.

    Don't worry about the Monsters doing weird things, I can sort them later, I am just worried about the bars not constraining to their parameters.

    The pickups, are mostly obvious, one for each, Power, Grenade, Bullet and Health.

    So if anyone want's to have a look at my .capx and let me know how, and or where I implement the clamp function I would be very thankful.

    Karl

  • You just need to make sure the values stay between 0 and maxvalue. At the moment you can shoot with 0 ammo and the ammo goes into negative values. You could put an 'ammo > 0' condition alongside 'on left mouse clicked' to stop that happening.

    And when you add or subtract from one of the values... Instead of adding 25 to player.ammo, set player.ammo to min(player.ammo + 25, player.maxAmmo).

    On grenade pickup set player.grenade to min(player.grenade + 3, player.maxGrenade) etc.

    powerbars.capx (r99)

  • Thanks for that ramones, I will have a proper look at that later, I've been up for about 36 hours now, and my brain is fried, lol

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Haha :) I know how you feel!

  • I may be off but you could use a,

    system everytick if(ammo >= 0)then ammo = 0

    to prevent going into the negative on that variable from firing if you are out of ammo. that less than or equal to equation messed me up on some debugging before. (yesterday, lol)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)