我试图用新变量(新时间)重置我的倒数计时器,但我做的任何事情似乎都没有用。有谁知道怎么做?
private long cdTime = 30000;
private long percentageTime = 30;
private void createCDTimer(){
mCountDownTimer = new CountDownTimer(cdTime, 1000) {
public void onTick(long millisUntilFinished) {
score = millisUntilFinished / 1000;
percentage = percentage - (100 / percentageTime);
cdTime = millisUntilFinished;
percentageTime = millisUntilFinished / 1000;
}
public void onFinish() {
score = 0;
percentage = 0;
}
}.start();
}
this code is in another class/void {
mCountDownTimer.cancel();
cdTime = cdTime + 20000;
percentageTime = percentageTime + 20;
mCountDownTimer.start();
}
提前致谢。
修改
以下是我需要的其他代码:
public class GameView extends SurfaceView implements Runnable {
Context context;
private Thread gameThread = null;
private SurfaceHolder ourHolder;
private volatile boolean playing;
private boolean paused = true;
private Canvas canvas;
private Paint paint;
private long fps;
private long thisTimeFrame;
private int screenX;
private int screenY;
private int column1;
private int column2;
private int column3;
private float enemyMDrop;
private float enemyMY;
private float enemyLDrop;
private float enemyLY;
private float enemyRDrop;
private float enemyRY;
private Random randomGen = new Random();
private int loadBarAnim;
private float loadBarTimer;
private float gameTimer;
private long percentage = 100;
private CountDownTimer mCountDownTimer;
private long cdTime;
private long percentageTime;
private Compressor compressor;
private loadingBar loadBar;
private loadingBarAnimation loadBarAnimation;
private EnemyMid enemyM;
private EnemyLeft enemyL;
private EnemyLeft enemyR;
private float mTimer;
private float lTimer;
private float rTimer;
private float enemyMWait;
private float enemyLWait;
private float enemyRWait;
private float enemyMTimer;
private float enemyLTimer;
private float enemyRTimer;
private boolean enemyMGo;
private boolean enemyLGo;
private boolean enemyRGo;
private SoundPool soundPool;
private int deathSound = -1;
private int liveSound = -1;
private int shootSound = -1;
private int gameMusic = -1;
private long score;
String scoreText = "Score: ";
//String scoreView = String.format("%.02f", score);
private int lives = 3;
public GameView (Context context, int x, int y){
super(context);
this.context = context;
ourHolder = getHolder();
paint = new Paint();
screenX = x;
screenY = y;
prepeareLevel();
}
private void createCDTimer(){
mCountDownTimer = new CountDownTimer(cdTime, 1000) {
public void onTick(long millisUntilFinished) {
score = millisUntilFinished / 1000;
percentage = percentage - (100 / percentageTime);
cdTime = millisUntilFinished;
percentageTime = millisUntilFinished / 1000;
}
public void onFinish() {
score = 0;
percentage = 0;
}
}.start();
}
private void prepeareLevel (){
cdTime = 30000;
percentageTime = 30;
createCDTimer();
loadBarAnim = 1;
column1 = screenX / 3;
column2 = (screenX / 3 ) * 2;
column3 = (screenX / 3 ) * 3;
compressor = new Compressor(context, screenX, screenY);
loadBar = new loadingBar(context, screenX, screenY);
loadBarAnimation = new loadingBarAnimation(context, screenX, screenY, 2);
enemyM = new EnemyMid(context, screenX, screenY);
enemyL = new EnemyLeft(context, screenX, screenY);
enemyR = new EnemyLeft(context, screenX, screenY);
mTimer = 0;
lTimer = 0;
rTimer = 0;
enemyMTimer = 2;
enemyLTimer = 0;
enemyRTimer = 5;
enemyMGo = false;
enemyLGo = false;
enemyRGo = false;
enemyMDrop = 0;
enemyLDrop = 0;
enemyRDrop = 0;
enemyMWait = randomGen.nextInt((5 - 1) + 1);
enemyLWait = randomGen.nextInt((5 - 1) + 1);
enemyRWait = randomGen.nextInt((5 - 1) + 1);
}
@Override
public void run(){
while (playing){
long startFrameTime = System.currentTimeMillis();
if(!paused){
update();
}
draw();
thisTimeFrame = System.currentTimeMillis() - startFrameTime;
if(thisTimeFrame >= 1){
fps = 1000/thisTimeFrame;
}
}
}
private void update(){
boolean finished = false;
if (finished){
}
}
private void draw(){
if(ourHolder.getSurface().isValid()){
canvas = ourHolder.lockCanvas();
if(rTimer < 30){
rTimer++;
}
else {
rTimer = 0;
if (enemyRTimer >= enemyRWait-1){
enemyRGo = true;
enemyRTimer = 0;
}
else if (!enemyRGo){
enemyRTimer++;
}
}
if(mTimer < 30){
mTimer++;
}
else {
mTimer = 0;
if (enemyMTimer >= enemyMWait-1){
enemyMGo = true;
enemyMTimer = 0;
}
else if (!enemyMGo){
enemyMTimer++;
}
}
if(lTimer < 30){
lTimer++;
}
else {
if (enemyLTimer >= enemyLWait-1){
enemyLGo = true;
enemyLTimer = 0;
}
else if (!enemyLGo){
enemyLTimer++;
}
}
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
if (enemyMGo){
enemyMDrop = enemyMDrop - 10;
enemyMY = enemyM.getY() - enemyMDrop;
}
if (enemyMY > 1200) {
enemyMDrop = 0;
enemyMY = 0;
enemyMGo = false;
enemyMTimer = 0;
enemyMWait = randomGen.nextInt((5 - 1) + 1);
mCountDownTimer.cancel();
cdTime = cdTime + 20000;
percentageTime = percentageTime + 20;
mCountDownTimer = new CountDownTimer(cdTime, 1000) {
public void onTick(long millisUntilFinished) {
score = millisUntilFinished / 1000;
percentage = percentage - (100 / percentageTime);
cdTime = millisUntilFinished;
percentageTime = millisUntilFinished / 1000;
}
public void onFinish() {
score = 0;
percentage = 0;
}
};
mCountDownTimer.start();
}
if (enemyLGo){
enemyLDrop = enemyLDrop - 10;
enemyLY = enemyL.getY() - enemyLDrop;
}
if (enemyLY > 1200) {
enemyLDrop = 0;
enemyLY = 0;
enemyLGo = false;
enemyLTimer = 0;
enemyLWait = randomGen.nextInt((5 - 1) + 1);
}
if (enemyRGo){
enemyRDrop = enemyRDrop - 10;
enemyRY = enemyR.getY() - enemyRDrop;
}
if (enemyRY > 1200) {
enemyRDrop = 0;
enemyRY = 0;
enemyRGo = false;
enemyRTimer = 0;
enemyRWait = randomGen.nextInt((5 - 1) + 1);
}
//Draw the invaders
canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);
canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);
canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);
//draw the compressor
canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);
//draw the animated loading bar
if (loadBarTimer > 2){
loadBarTimer = 0;
if (loadBarAnim == 1){
loadBarAnim = 2;
}
else if (loadBarAnim == 2){
loadBarAnim = 3;
}
else if (loadBarAnim == 3){
loadBarAnim = 4;
}
else {
loadBarAnim = 1;
}
}
else {
loadBarTimer++;
}
if (loadBarAnim == 1) {
canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
}
else if (loadBarAnim == 2){
canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
}
else if (loadBarAnim == 3) {
canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
}
else {
canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
}
//draw the loading bar
canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);
//Draw the Score
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Time: " + score, 100, 1600, paint);
//Draw the Percentage
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Percentage: " + percentage, 725, 1600, paint);
ourHolder.unlockCanvasAndPost(canvas);
}
}
//Paused or Stopped
public void pause(){
playing = false;
try{
gameThread.join();
}
catch (InterruptedException e){
Log.e("Error: ", "Error joining thread");
}
}
//Resume
public void resume(){
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
public boolean onTouchEvent(MotionEvent motionEvent){
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
//player touches the screen
case MotionEvent.ACTION_DOWN:
paused = false;
if (motionEvent.getX() > screenX - column1 && enemyRY > 200){
if (motionEvent.getY() < screenY) {
enemyRWait = randomGen.nextInt((5 - 1) + 1);
enemyRY = 0;
enemyRDrop = 0;
enemyRGo = false;
enemyRTimer = 0;
rTimer = 0;
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
//Draw the invaders
canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);
canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);
canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);
//draw the compressor
canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);
//draw the animated loading bar
if (loadBarTimer > 2){
loadBarTimer = 0;
if (loadBarAnim == 1){
loadBarAnim = 2;
}
else if (loadBarAnim == 2){
loadBarAnim = 3;
}
else if (loadBarAnim == 3){
loadBarAnim = 4;
}
else {
loadBarAnim = 1;
}
}
else {
loadBarTimer++;
}
if (loadBarAnim == 1) {
canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
}
else if (loadBarAnim == 2){
canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
}
else if (loadBarAnim == 3) {
canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
}
else {
canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
}
//draw the loading bar
canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);
//Draw the Score
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Time: " + score, 100, 1600, paint);
//Draw the Percentage
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Percentage: " + percentage, 725, 1600, paint);
ourHolder.unlockCanvasAndPost(canvas);
}
}
if (motionEvent.getX() > screenX - column2 && motionEvent.getX() < screenX - column1 && enemyMY > 200){
if (motionEvent.getY() < screenY) {
enemyMWait = randomGen.nextInt((5 - 1) + 1);
enemyMY = 0;
enemyMDrop = 0;
enemyMGo = false;
enemyMTimer = 0;
mTimer = 0;
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
//Draw the invaders
canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);
canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);
canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);
//draw the compressor
canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);
//draw the animated loading bar
if (loadBarTimer > 2){
loadBarTimer = 0;
if (loadBarAnim == 1){
loadBarAnim = 2;
}
else if (loadBarAnim == 2){
loadBarAnim = 3;
}
else if (loadBarAnim == 3){
loadBarAnim = 4;
}
else {
loadBarAnim = 1;
}
}
else {
loadBarTimer++;
}
if (loadBarAnim == 1) {
canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
}
else if (loadBarAnim == 2){
canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
}
else if (loadBarAnim == 3) {
canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
}
else {
canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
}
//draw the loading bar
canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);
//Draw the Score
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Time: " + score, 100, 1600, paint);
//Draw the Percentage
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Percentage: " + percentage, 725, 1600, paint);
ourHolder.unlockCanvasAndPost(canvas);
}
}
if (motionEvent.getX() > screenX - column3 && motionEvent.getX() < screenX - column2 && enemyLY > 200){
if (motionEvent.getY() < screenY) {
enemyLWait = randomGen.nextInt((5 - 1) + 1);
enemyLY = 0;
enemyLDrop = 0;
enemyLGo = false;
enemyLTimer = 0;
lTimer = 0;
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
//Draw the invaders
canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);
canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);
canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);
//draw the compressor
canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);
//draw the animated loading bar
if (loadBarTimer > 2){
loadBarTimer = 0;
if (loadBarAnim == 1){
loadBarAnim = 2;
}
else if (loadBarAnim == 2){
loadBarAnim = 3;
}
else if (loadBarAnim == 3){
loadBarAnim = 4;
}
else {
loadBarAnim = 1;
}
}
else {
loadBarTimer++;
}
if (loadBarAnim == 1) {
canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
}
else if (loadBarAnim == 2){
canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
}
else if (loadBarAnim == 3) {
canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
}
else {
canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
}
//draw the loading bar
canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);
//Draw the Score
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Time: " + score, 100, 1600, paint);
//Draw the Percentage
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Percentage: " + percentage, 725, 1600, paint);
ourHolder.unlockCanvasAndPost(canvas);
}
}
break;
}
return true;
}
}
错误代码:
11-22 06:41:46.164 9091-9146 / au.com.webanddesignbros.loadingbarsimulator W / dalvikvm:threadid = 12:线程退出时未捕获异常(组= 0xa65f7228)
11-22 06:41:46.174 9091-9146 / au.com.webanddesignbros.loadingbarsimulator E / AndroidRuntime:FATAL EXCEPTION:Thread-206
java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序 在android.os.Handler。(Handler.java:121)
在android.os.CountDownTimer $ 1。(CountDownTimer.java:109)
在android.os.CountDownTimer。(CountDownTimer.java:109)
在au.com.webanddesignbros.loadingbarsimulator.GameView $ 2.(GameView.java:0)
at au.com.webanddesignbros.loadingbarsimulator.GameView.draw(GameView.java:250)
在au.com.webanddesignbros.loadingbarsimulator.GameView.run(GameView.java:164)
在java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
您正在取消倒计时并在不设置新时间值的情况下重新开始倒计时。
实际上,您只需更改cdTime
和percentageTime
个变量。您应该使用这些更新的值创建一个新的倒数计时器。
mCountDownTimer.cancel();
cdTime = cdTime + 20000;
percentageTime = percentageTime + 20;
//Create a countdown timer with the updated time
mCountDownTimer = new CounDownTimer(cdTime, anInterval){
/* handle the countdown notifications as you wish, for example like you did in your question */
}
mCountDownTimer.start();
为了验证上面的代码是否有效,我们已经制作了测试应用程序。代码和apk可在此存储库中找到:https://github.com/ySiggen/CDTimerTest
在问题中添加了堆栈跟踪后,此答案已使用以下内容进行了编辑:
您获得的错误意味着您正在从后台线程调用代码(倒计时器),而应该从主线程完成。
您可以使用Handler从另一个线程调用代码:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mCountDownTimer = new CountDownTimer(cdTime, 1000) {
/* handle the countdown notifications as you wish, for example like you did in your question */
}.start();
}
});