我有一个游戏,其中图像视图在计时器上发生变化。单击按钮(停止)时,将调用更新以更新分数和旋转。我需要在whammy()时停止;调用并运行帧动画,然后在动画结束时启动计时器。我试着打电话给睡觉(2500);但它会导致所有内容等待2.5秒,然后帧动画随图像视图计时器一起运行。这对我需要的东西不起作用。以下是我需要帮助的代码部分:
public void onClick(View v) {
if (v.getId() == R.id.btstop) {
final Button bstop = (Button) findViewById(R.id.btstop);
bstop.setEnabled(false);
updateState();
}
}
/**
* set random view. get random 1 point from class utils and display to 1
* position random
*/
public void setRandomView() {
int item = new Random().nextInt(18);
current = Utils.getAPoint();
int img = current.getImg();
int score = current.getScore();
int spin = current.getSpin();
iv[item].setImageResource(img);
dataPoint.get(item).setImg(img);
dataPoint.get(item).setScore(score);
dataPoint.get(item).setSpin(spin);
ivcenter.setImageResource(img);
}
/**
* display Name,score & spin to screen
*/
public void setPoint() {
tvspin.setText(spins + "\n" + name);
tvpoint.setText(point + "");
}
/**
* Start Thread Sets the speed of the game board Enable STOP button to true
*
* @param time
*/
public void start(int time) {
countDownTimer = new CountDownTimer(time, 300) {
@Override
public void onTick(long millisUntilFinished) {
// call to set random view
setRandomView();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
/**
* stop timer thread
*/
public void stop() {
try {
countDownTimer.cancel();
} catch (Exception e) {
// TODO: handle exception
} finally {
}
}
/**
* update state game and check what tile is in the center
*/
public void updateState() { // get score,spin current point
int scoreCurrent = current.getScore();
int spinCurrent = current.getSpin();
spins--;// Take one from spins
// if score = 2, that mean to double score
if (scoreCurrent == 2) {
point = point * 2;
playmp3(R.raw.correct);
}
// if score = 0, that means tile is a whammy
if (scoreCurrent == 0) {
playmp3(R.raw.buzz);
whammy();// I need to put everything else on hold here and animation here
point = 0;// sets the score to 0
whammy++; // Add a whammy to the whammy count
// Display the whammy that was added
for (int i = 0; i < whammy; i++) {
popup[i].setVisibility(View.VISIBLE);
}
// sleep(2500);
if (whammy == 4) {
stop();
stopmp3();
callFinish();
}
}
// else ,score = score current + point.getscore
else {
playmp3(R.raw.correct);
point = point + scoreCurrent;
}
// spins--;// Take one from spint
// if spin > 0, that mean is add a spin
if (spinCurrent > 0) {
spins = spins + spinCurrent;
}
setPoint(); // display spin,score and name to screen
sleep(2000); // Adds a delay so that the whammy pop up can be seen
// if spint >0 start new data random
if (spins > 0) {
stop();
final Button bstop = (Button) findViewById(R.id.btstop);
bstop.setEnabled(true);
start(10000 * 60 * 60 * 24);
} else {
// stop all
stop();
stopmp3();
// if spin < 2, that round <2 move to spin activity
if (round < 2) {
Intent it = new Intent(getBaseContext(), SpinActivity.class);
startActivity(it);
finish();
}
// else move to high score activity
else {
callFinish();
}
}
}
private void whammy() {
// set up the whammy act animation
final ImageView act = (ImageView) findViewById(R.id.whammyact);
act.setBackgroundResource(R.drawable.tnt_animation);
actAnim = (AnimationDrawable) act.getBackground();
actAnim.setVisible(true, true);
}
答案 0 :(得分:0)
确定以下是在游戏计时器重新开始之前使动画运行并完成所做的更改...
如果有问题或任何人需要帮助,请自由询问。
if (scoreCurrent == 0) {
countDownTimer.cancel();
countDownTimer = null;
// Use Timer to set visibility to GONE after the animation finishes.
TimerTask timerTask = new TimerTask(){
@Override
public void run() {
PressActivity.this.runOnUiThread(new Runnable(){
public void run() {
resumeLogicAfterPress();
}
});}};
Timer timer = new Timer();
timer.schedule(timerTask, getWhammyAnimationTotalDuration(actAnim));
} else {
resumeLogicAfterPress();
}
}
public void resumeLogicAfterPress() {
// Display the whammy that was added
for (int i = 0; i < whammy; i++) {
popup[i].setVisibility(View.VISIBLE);
}
sleep(2500); //Adds a delay so that the whammy pop up can be seen
//Start the background music
playmp3(mpPress, R.raw.press);
// if whammy = 4. stop all and move to high score
if (whammy == 4) {
stop();
stopmp3();
callFinish();
}
// if spint >0 start new data random
if (spint > 0) {
stop();
final Button bstop = (Button) findViewById(R.id.btstop);
bstop.setEnabled(true);
start(10000 * 60 * 60 * 24);
} else {
// stop all
stop();
stopmp3();
// if spin < 2, that round <2 move to spin activity
if (spin < 2) {
Intent it = new Intent(getBaseContext(), SpinActivity.class);
startActivity(it);
finish();
}
// esle move to high score activity
else {
callFinish();
}
}
}
private void whammy() {
// set up the whammy act animation
final ImageView act = (ImageView) findViewById(R.id.whammyact);
act.setBackgroundResource(R.drawable.tnt_animation);
actAnim = (AnimationDrawable) act.getBackground();
actAnim.setVisible(true, true);
}