首先,如果我没有正确发布,我很抱歉 - 首先发帖!
其次,我为下面显示的代码道歉,这是我尝试制作的第一个Android应用程序,这是我第一次在java中开发,所以请体谅:)
现在,情况是,我希望下图中显示的足球以不同的速度上下移动,从屏幕的顶部和底部反弹。我实施球的方式是我有3个球对象,一旦它们从屏幕左侧移出,就会移动到屏幕右侧。
我的问题是我正在努力寻找一种让球变速的方法,我觉得我过于复杂了。任何帮助都将不胜感激。
图片:http://tamenorth.com/screen.png
此处是我的代码:
public Ball(float x, float y, int width, int height, float scrollSpeed,
float groundY) {
super(x, y, width, height, scrollSpeed);
ball = new Circle();
this.groundY = groundY;
}
@Override
public void update(float delta) {
super.update(delta);
calculateY(rand);
}
private void calculateY(int rand) {
int velocity = rand;
if (positionY < midPointY + 50) {
positionY = positionY + velocity;
} else if (positionY >= midPointY + 50) {
velocity = velocity * -1;
positionY = positionY + velocity;
}
if (positionY < 0) {
velocity = Math.abs(velocity);
}
ball.set(position.x, position.y, 6.5f);
}
@Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX);
// Change the height to a random number
isScored = false;
}
public boolean collides(Bird bird) {
if (position.x < bird.getX() + bird.getWidth()) {
return (Intersector.overlaps(bird.getBoundingCircle(), ball));
}
return false;
}
public int getPositionY() {
rand = (int) (Math.random() * 2);
return positionY;
}
public boolean isScored() {
return isScored;
}
public void setScored(boolean b) {
isScored = b;
}
public void onRestart(float x, float scrollSpeed) {
velocity.x = scrollSpeed;
reset(x);
positionY = 0;
}