How do I Make a Fuel Meter? RESOLVED :)

0 favourites
  • 9 posts
From the Asset Store
You control a chopper that flies through a cave!
  • I'm trying to use a mask with the tween effect to create a fuel meter. The object is that when the car moves the fuel starts to go down and when you pick up fuel (Represented by the every 5 secs code for now) fuel gets added back into the tank when the tank hits zero it triggers game over. Now, there will be different sizes to pick up so I added a clamp to prevent it from going past more than 150.

    The issue I'm having is it will work a few times then stop, and then start to go up and just keep going up.

    I'm not married to this method so if there is a better way I'm all for it.

    This is what I have:

    https://www.dropbox.com/s/0paud0vgoniekln/MeterTest2.c3p?dl=0

    Thank you in advance for your help.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure if there's a particular reason you want to use tween here.

    This is all you need.

    + System: metermask.Height > 0
    -> metermask: Set height to metermask.Height-0.1
    
    + System: Every 5 seconds
    -> metermask: Set height to min(metermask.Height+25,150)
    

    Note that you will probably eventually want to keep something like the fuel level value in a variable, and set the mask height based on that variable.

  • Thanks, I will give it a try. I was trying the tween and the mask for the smoothness like your gas tank running low in a car. But I guess I could have just made it like you would a health bar or something.

    Thanks again.

  • If you're going for smoothness and want to use tween, you'll need to define an "update period", as tween is something that happens over time.

    You wouldn't tween something every tick, since that is no different then setting the position directly.

    You also generally don't want to use multiple overlapping tweens unless there's some specific effect you are going for.

    So to prevent overlapping tweens, such as your fuel draining normally, and your fuel filling from pickup, you'll need some additional information. All tweens need a start point and an end point. Assuming your fuel is still draining during the time it takes to fill up, you'll want to find out how much fuel would have been drained during the time it takes for your fill up tween and subtract that from the end amount. When it's done filling, then start a new emptying tween from that point to 0. But then you would need to set a new time, based on the amount of fuel you currently have, since fuel drain should be constant...

    Anyways I still think tween isn't the way to go here.

    Just have a fuel variable that is subtracted from (dt amount) every tick for a constant smooth rate. And if you want your fill up to be smooth do the same thing, and add fuel at a constant rate every tick until the total amount added runs out (you can keep this in a separate variable).

  • Understood, thank you, I was just up working that method out as well.

    Thank you.

  • Thank you for your help oosyrag. I see I made this wayyyyyy harder than it had to be, but the knowledge you shared made all the difference.

    I'll try not to overthink the process next time. (no promises) but I will try.

    Meter Height > 0 metermask.Height-0.05

    Player on collision with fuel * metermask.Height * min(metermask.Height+25,150)

    I'll probably play around with it a little more but this will work.

    Thank you...

  • Here's the same, with gradual filling. metermask.reserve is an instance variable used to keep track of how much/long to fill up.

    + System: metermask.Height > 0
    -> metermask: Set height to max(0,metermask.Height-0.05)
    
    + System: metermask.reserve > 0
    -> metermask: Set reserve to max(0,metermask.reserve-1)
    -> metermask: Set height to min(150,metermask.Height+1)
    
    + Touch: On any touch start
    -> metermask: Set reserve to metermask.reserve+25
    

    You can add stuff like setting the reserve to 0 if fuel gets maxed out, if that is your desired behavior. Alternatively, you can set the reserve to min(25,fuelmax-currentfuel), to take the smaller of either 25 or the difference between max fuel and current fuel to prevent overfilling.

  • Wow! Thank you so much, this was very unexpected of your time. I'm sure this will greatly help some others as well.

    I do appreciate it.

    Thank you again.

  • Here's the same, with gradual filling. metermask.reserve is an instance variable used to keep track of how much/long to fill up.

    > + System: metermask.Height > 0
    -> metermask: Set height to max(0,metermask.Height-0.05)
    
    + System: metermask.reserve > 0
    -> metermask: Set reserve to max(0,metermask.reserve-1)
    -> metermask: Set height to min(150,metermask.Height+1)
    
    + Touch: On any touch start
    -> metermask: Set reserve to metermask.reserve+25
    

    You can add stuff like setting the reserve to 0 if fuel gets maxed out, if that is your desired behavior. Alternatively, you can set the reserve to min(25,fuelmax-currentfuel), to take the smaller of either 25 or the difference between max fuel and current fuel to prevent overfilling.

    ==============================================

    I Just tested it this works perfectly!!! And it is smooth!!!

    Thank you so much...

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