我不知道这段代码有什么问题。当精灵到达右端角时,它应该向左移动。但是这段代码会在(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;
}