gpt4 book ai didi

java - 当我点击 "Generate"按钮时只工作一次

转载 作者:行者123 更新时间:2023-11-29 18:35:35 24 4
gpt4 key购买 nike

我的按钮有问题,例如,当我点击按钮 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) {

}
}

最佳答案

您的问题是,当您首先单击按钮时,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();
}
});

关于java - 当我点击 "Generate"按钮时只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com