[英]Android libgdx… moving of sprite back and forth horizontally
I don't know what's wrong with this code. 我不知道这段代码有什么问题。 The sprite supposed to move at the to the left when it reaches the right end corner.
当精灵到达右端角时,它应该向左移动。 but this code does when it (sprite) reaches the right end corner, the sprite just stops.
但是这段代码会在(sprite)到达右端角时执行,sprite才停止。
The startM is the left end corner, and the endM is the right end corner
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() );
Make small change in your code. 对您的代码进行少量更改。
if(endM == ballMeter.getX()){
leftEnd =true;
}
Convert to 转换成
if(endM <= ballMeter.getX()){
leftEnd =true;
}
And 和
if(startM == ballMeter.getX()){
leftEnd = false;
}
to 至
if(startM >= ballMeter.getX()){
leftEnd = false;
}
Thank you @AbhishekAryan 谢谢@AbhishekAryan
if(leftEnd == false || (startM <= ballMeter.getX()) ) {
position.x = ballMeter.getX();
position.x += 20 * delta;
if(endM <= ballMeter.getX()){
leftEnd =true;
}
}
if(leftEnd) {
position.x = ballMeter.getX();
position.x -= 20 * delta;
if(startM >= ballMeter.getX()){
leftEnd = false;
}
}
ballMeter.setPosition(position.x , ballMeter.getY() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.