我正在尝试用三个球从顶部向下打到一个游戏。
我将animation.setDuration中球的持续时间设置为5000。
但是,只要分数增加10,我就想将速度加100。
我该怎么做?我应该循环吗?
Btw代码不完整,因为我简短而简洁。
//当得分为10时,我希望速度减去100,这样它会更快,因此它会变为4900 //当得分为20时我希望速度减去100,因此它将达到4800 //当得分为30时我希望速度减去100,因此它将达到4700
public class MainActivity extends Activity implements Runnable {
private ImageView ball1;
private ImageView ball2;
private ImageView ball3;
private TextView mtvscore;
private int Score = 00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball1 = (ImageView) findViewById(R.id.ball1);
ball2 = (ImageView) findViewById(R.id.ball2);
ball3 = (ImageView) findViewById(R.id.ball3);
mtvscore = (TextView) findViewById(R.id.score);
mtvscore.setText("" + Score);
mhandle = new Handler();
mthread = new Thread(this);
mthread.start();
private void startBallAnimation() {
final TranslateAnimation animation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.86f
);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mhandle.post(new Runnable() {
@Override
public void run() {
setBallColors();
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(final Animation animation) {
if(ball1color== WORDS_TO_COLORS.get(word1text) && ball2color== WORDS_TO_COLORS.get(word2text) &&
ball3color== WORDS_TO_COLORS.get(word3text)){
Score++;
sound.playscoreupsound();
}else {
Score--;
sound.playgameosound();
}
mhandle.post(new Runnable() {
@Override
public void run() {
mtvscore.setText(String.valueOf(Score));
setBallColors();
restartBallAnimation(animation);
}
});
}
});
restartBallAnimation(animation);
}
private void restartBallAnimation(final Animation animation) {
animation.setDuration(5000);
ball1.startAnimation(animation);
ball2.startAnimation(animation);
ball3.startAnimation(animation);
}
private void setBallColors() {
ball1color = COLORS.get(random.nextInt(COLORS.size()));
ball2color = COLORS.get(random.nextInt(COLORS.size()));
ball3color = COLORS.get(random.nextInt(COLORS.size()));
ball1.setImageDrawable(ballDrawable(ball1color));
ball2.setImageDrawable(ballDrawable(ball2color));
ball3.setImageDrawable(ballDrawable(ball3color));
}
@Override
public void run() {
startBallAnimation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
请注意,如果您的score
> = 500,则无法看到动画,因为它太快了。
private int Score = 0;
private int speed = 5000;
/*
In Java, both two operands (Score and 10) are integers, so the result will be integer
when
Score = 1 , (5000- (1/10) * 100 ) -> (5000- 0 * 100 ) = 5000
Score = 5, (5000- (5/10) * 100 ) -> (5000- 0 * 100 ) = 5000
Score = 10, (5000- (10/10) * 100 ) -> (5000- 1 * 100 ) = 4900
Score = 25, (5000- (25/10) * 100 ) -> (5000- 2 * 100 ) = 4800
Score = 30, (5000- (30/10) * 100 ) -> (5000- 3 * 100 ) = 4700
Score = 35, (5000- (35/10) * 100 ) -> (5000- 3 * 100 ) = 4700
*/
private void restartBallAnimation(final Animation animation) {
animation.setDuration(speed - (Score/10) * 100 );
ball1.startAnimation(animation);
ball2.startAnimation(animation);
ball3.startAnimation(animation);
}