IF statement with positions x

0

Good morning, I want to mount an if statement as follows:

I have the xInicial position and the xFinal position.

My goal is for the object to go from xInitial to xFinal, adding in positive and when it reaches xFinal, add up in negative and when it returns to the Initial x, more of the same.

In summary, go from xInicial to XFinal as if it bounced.

Any ideas?

   xinicial = 0;
   xfinal = 5;
    void update(){
       if(xinicial < xfinal){ 
          xinicial += 1;
       }
   }

The idea is that when xInicial is = 5 then subtract to 0 and then again to add, infinitely.

    
asked by PiledroMurcia 09.05.2017 в 10:08
source

1 answer

0

For what you want to do you need a repetitive, in this case a while , since a if is a condition that is repeated only once. That is, your code would only add 1 to xInitial . I do not know exactly when you want it to be bouncing, but assuming it's always bouncing it would be something like this:

int xinicial = 0;
int xfinal = 5;
int objeto=0;
boolean vuelta=false;
 void update(){
   while(true){
      if(xinicial<xfinal && !vuelta){
         objeto++;
        }else if(xinicial==xfinal){
          vuelta=true;
          objeto--;
        }else if(xinicial>0 && vuelta){
          objeto--;
        }else{
          vuelta=false;
        }
      System.out.println(objeto);
   }

In the while(true) you have to put the condition that has to be met to keep it running, in this case, as I said, it's going to run all the time I've already set true so it's going to always meet.

    
answered by 09.05.2017 / 10:41
source