例如,当我单击StartGame按钮“重试”时,我的按钮出现问题,然后在生成按钮不起作用的情况下玩游戏。
我认为发生此问题是因为该按钮位于其他代码之前的oncreate方法内部
package com.forfree.com.robux;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// Frame
private FrameLayout gameFrame;
private int frameHeight, frameWidth, initialFrameWidth;
private LinearLayout startLayout;
Button generate;
// Image
private ImageView box, black, orange, pink;
private Drawable imageBoxRight, imageBoxLeft;
// Size
private int boxSize;
// Position
private float boxX, boxY;
private float blackX, blackY;
private float orangeX, orangeY;
private float pinkX, pinkY;
// Score
private TextView scoreLabel, highScoreLabel;
private int score, highScore, timeCount;
private SharedPreferences settings;
// Class
private Timer timer;
private Handler handler = new Handler();
private SoundPlayer soundPlayer;
// Status
private boolean start_flg = false;
private boolean action_flg = false;
private boolean pink_flg = false;
private static float width;
int progressState = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////// THIS IS MY BUTTON THAT WORK ONCE THIME
generate = (Button) findViewById(R.id.generate);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showProgressDialog();
}
});
}
public void showProgressDialog(){
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Congrats you win "+score+" Robux");
pDialog.setCancelable(false);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setProgress(0);
pDialog.setMax(100);
pDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
while( progressState < 100 ){
progressState += 10;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pDialog.setProgress(progressState);
}
if (progressState>= 100)
pDialog.dismiss();
}
}).start();
////////
soundPlayer = new SoundPlayer(this);
gameFrame = findViewById(R.id.gameFrame);
startLayout = findViewById(R.id.startLayout);
box = findViewById(R.id.box);
black = findViewById(R.id.black);
orange = findViewById(R.id.orange);
pink = findViewById(R.id.pink);
scoreLabel = findViewById(R.id.scoreLabel);
highScoreLabel = findViewById(R.id.highScoreLabel);
imageBoxLeft = getResources().getDrawable(R.drawable.box_left);
imageBoxRight = getResources().getDrawable(R.drawable.box_right);
// High Score
settings = getSharedPreferences("GAME_DATA", Context.MODE_PRIVATE);
highScore = settings.getInt("HIGH_SCORE", 0);
highScoreLabel.setText("High Score : " + highScore);
}
//
//
public void changePos() {
// Add timeCount
timeCount += 20;
// Orange
orangeY += 12;
float orangeCenterX = orangeX + orange.getWidth() / 2;
float orangeCenterY = orangeY + orange.getHeight() / 2;
if (hitCheck(orangeCenterX, orangeCenterY)) {
orangeY = frameHeight + 100;
score += 233;
soundPlayer.playHitOrangeSound();
}
if (orangeY > frameHeight) {
orangeY = -100;
orangeX = (float) Math.floor(Math.random() * (frameWidth - orange.getWidth()));
}
orange.setX(orangeX);
orange.setY(orangeY);
// Pink
if (!pink_flg && timeCount % 10000 == 0) {
pink_flg = true;
pinkY = -20;
pinkX = (float) Math.floor(Math.random() * (frameWidth - pink.getWidth()));
}
if (pink_flg) {
pinkY += 20;
float pinkCenterX = pinkX + pink.getWidth() / 2;
float pinkCenterY = pinkY + pink.getWidth() / 2;
if (hitCheck(pinkCenterX, pinkCenterY)) {
pinkY = frameHeight + 30;
score += 30;
// Change FrameWidth
if (initialFrameWidth > frameWidth * 110 / 100) {
frameWidth = frameWidth * 110 / 100;
changeFrameWidth(frameWidth);
}
soundPlayer.playHitPinkSound();
}
if (pinkY > frameHeight) pink_flg = false;
pink.setX(pinkX);
pink.setY(pinkY);
}
// Black
blackY += 18;
float blackCenterX = blackX + black.getWidth() / 2;
float blackCenterY = blackY + black.getHeight() / 2;
if (hitCheck(blackCenterX, blackCenterY)) {
blackY = frameHeight + 100;
// Change FrameWidth
frameWidth = frameWidth * 80 / 100;
changeFrameWidth(frameWidth);
soundPlayer.playHitBlackSound();
if (frameWidth <= boxSize) {
gameOver();
}
}
if (blackY > frameHeight) {
blackY = -100;
blackX = (float) Math.floor(Math.random() * (frameWidth - black.getWidth()));
}
black.setX(blackX);
black.setY(blackY);
// Move Box
if (action_flg) {
// Touching
boxX += 14;
box.setImageDrawable(imageBoxRight);
} else {
// Releasing
boxX -= 14;
box.setImageDrawable(imageBoxLeft);
}
// Check box position.
if (boxX < 0) {
boxX = 0;
box.setImageDrawable(imageBoxRight);
}
if (frameWidth - boxSize < boxX) {
boxX = frameWidth - boxSize;
box.setImageDrawable(imageBoxLeft);
}
box.setX(boxX);
scoreLabel.setText(" Robux Collected : " + score);
}
//
//
public boolean hitCheck(float x, float y) {
if (boxX <= x && x <= boxX + boxSize &&
boxY <= y && y <= frameHeight) {
return true;
}
return false;
}
public void changeFrameWidth(int frameWidth) {
ViewGroup.LayoutParams params = gameFrame.getLayoutParams();
params.width = frameWidth;
gameFrame.setLayoutParams(params);
}
public void gameOver() {
// Stop timer.
timer.cancel();
timer = null;
start_flg = false;
// Before showing startLayout, sleep 1 second.
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
changeFrameWidth(initialFrameWidth);
startLayout.setVisibility(View.VISIBLE);
box.setVisibility(View.INVISIBLE);
black.setVisibility(View.INVISIBLE);
orange.setVisibility(View.INVISIBLE);
pink.setVisibility(View.INVISIBLE);
// Update High Score
if (score > highScore) {
highScore = score;
highScoreLabel.setText("Robux Getting : " + highScore);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("HIGH_SCORE", highScore);
editor.commit();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (start_flg) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
action_flg = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
action_flg = false;
}
}
return true;
}
public void startGame(View view) {
start_flg = true;
startLayout.setVisibility(View.INVISIBLE);
if (frameHeight == 0) {
frameHeight = gameFrame.getHeight();
frameWidth = gameFrame.getWidth();
initialFrameWidth = frameWidth;
boxSize = box.getHeight();
boxX = box.getX();
boxY = box.getY();
}
frameWidth = initialFrameWidth;
box.setX(0.0f);
black.setY(3000.0f);
orange.setY(3000.0f);
pink.setY(3000.0f);
blackY = black.getY();
orangeY = orange.getY();
pinkY = pink.getY();
box.setVisibility(View.VISIBLE);
black.setVisibility(View.VISIBLE);
orange.setVisibility(View.VISIBLE);
pink.setVisibility(View.VISIBLE);
timeCount = 0;
score = 0;
scoreLabel.setText("Score : 0");
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (start_flg) {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}
}, 0, 20);
}
@Override
public void onClick(View v) {
}
}
答案 0 :(得分:2)
您的问题是,当您首先单击按钮时,progressState
最多可计数100并关闭对话框。
每次再次单击该按钮,progressState
仍保持100以上。
在按钮的onClick方法中重置progressState
。
它应该像这样:
generate = (Button) findViewById(R.id.generate);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressState = 0;
showProgressDialog();
}
});