vendredi 20 juillet 2018

How do I turn the normal kind of "if-elsif" statements to the sideways kind? (Ruby)

This is what a normal kind of if-elsif-else statement look like:

    if button_down?(@buttons[:forward]) and @speed < @max_speed
        @speed += @acceleration 
    elsif button_down?(@buttons[:backward]) and @speed > -@max_speed
        @speed -= @acceleration
    end

Now, I want to turn it to something efficient like this:

    @speed += @acceleration if button_down?(@buttons[:forward]) and @speed < @max_speed
    @speed -= @acceleration elsif button_down?(@button[:forward]) and @speed > -@max_speed

This code^ always comesback with a traceback-

syntax error, unexpected keyword_elsif, expecting end-of-input.

How can this be made in a correct way? Going with If always?

Aucun commentaire:

Enregistrer un commentaire