简体   繁体   中英

Change Image Using IF Statements

I have setup a fuel bar in my game, but I am trying to use if statements to change the bar image based on how low the fuel percentage is. For some reason, the image is not changing along with the percentage. As such, the bar remains full. What is going on here?

fuelRemaining = 100;
    FuelRemaining.text = [NSString stringWithFormat:@"%i%%", fuelRemaining];
    FuelBar.image = [UIImage imageNamed:@"fuel_bar_100.png"];
    if (fuelRemaining > 90) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_100.png"];
    }
    else if (fuelRemaining > 80 && fuelRemaining <= 90) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_90.png"];
    }
    else if (fuelRemaining > 70 && fuelRemaining <= 80) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_80.png"];
    }
    else if (fuelRemaining > 60 && fuelRemaining <= 70) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_70.png"];
    }
    else if (fuelRemaining > 50 && fuelRemaining <= 60) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_60.png"];
    }
    else if (fuelRemaining > 40 && fuelRemaining <= 50) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_50.png"];
    }
    else if (fuelRemaining > 30 && fuelRemaining <= 40) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_40.png"];
    }
    else if (fuelRemaining > 20 && fuelRemaining <= 30) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_30.png"];
    }
    else if (fuelRemaining > 10 && fuelRemaining <= 20) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_20.png"];
    }
    else if (fuelRemaining > 0 && fuelRemaining <= 10) {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_10.png"];
    }
    else {
        FuelBar.image = [UIImage imageNamed:@"fuel_bar_0.png"];
    }

First of all, simplify that with some basic math:

int fuelRounded = 10.0 * floor((fuelRemaining/10.0)+0.5);
NSString *fuelImageString = [NSString stringWithFormat:@"fuel_bar_%d.png", fuelRounded];
FuelBar.image = [UIImage imageNamed:fuelImageString];

(The math may need tweaking depending on your game logic and possible values for fuelRemaining .)

Second, it looks like you're setting fuelRemaining to 100 at the top of the method. You'll need to set the image again after fuelRemaining has changed to something else.

For example:

- (void) updateFuelViews {
    FuelRemaining.text = [NSString stringWithFormat:@"%i%%", fuelRemaining];

    int fuelRounded = 10.0 * floor((fuelRemaining/10.0)+0.5);
    NSString *fuelImageString = [NSString stringWithFormat:@"fuel_bar_%d.png", fuelRounded];
    FuelBar.image = [UIImage imageNamed:fuelImageString];
}

Then call [self updateFuelViews]; every time fuelRemaining changes.

You may want to research Key-Value Observing , which would let you update the fuel views automatically whenever fuelRemaining changes. It's a bit of a headache, but could make it easier to manage if you have lots of stuff changing constantly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM