我不知道这段代码有什么问题。当精灵到达右端角时,它应该向左移动。但是这段代码会在(sprite)到达右端角时执行,sprite才停止。


startM是左端角,endM是右端角


 //in constructor
 position = new Vector2(ShootingTreys.WIDTH*0.48f, ShootingTreys.HEIGHT*0.025f);

  // in update
 delta = Gdx.graphics.getDeltaTime();
 if(ps.touch == false){

        if(leftEnd == false && (startM <= ballMeter.getX()) ){
            position.x = ballMeter.getX();
            position.x += 20 *delta;

            if(endM == ballMeter.getX()){
                leftEnd =true;
            }
        }
        else {
            position.x = ballMeter.getX();
            position.x -= 20 *delta;

            if(startM == ballMeter.getX()){
                leftEnd = false;
            }
        }

        ballMeter.setPosition(position.x , ballMeter.getY() );

最佳答案

对您的代码进行少量更改。

if(endM == ballMeter.getX()){
       leftEnd =true;
}


转换成

if(endM <= ballMeter.getX()){
       leftEnd =true;
}




if(startM == ballMeter.getX()){
   leftEnd = false;
}




if(startM >= ballMeter.getX()){
       leftEnd = false;
}

10-06 12:51