我正在创建一个Android游戏,在增加分数时遇到了问题。
因此,有2个对象,一个放置在屏幕中间,一个对象掉到屏幕底部。
当掉落的物体通过屏幕中间的物体时,它将添加一个点。
当我将下降物体的速度设置为skullY + = 5或skullY + = 10时,一切都很好,并且将分数添加到了分数中,但是如果我将速度更改为skullY + = 7或skullY + = 12,它将会更快,并且通过时不会添加分数。
这是我目前遇到的问题。
这是我的活动代码:
//BOX AND SKULL X AND Y
private float boxX, boxY;
private float skullX, skullY;
private float skullX1, skullY1;
//BOX SKULL PICTURES
private ImageView box;
private ImageView skull, skull1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_state);
decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
frame = (RelativeLayout) findViewById(R.id.frame);
txtTimer = (TextView) findViewById(R.id.txtTimer);
txtScore = (TextView) findViewById(R.id.txtScore);
box = (ImageView) findViewById(R.id.box);
skull = (ImageView) findViewById(R.id.skull);
skull1 = (ImageView) findViewById(R.id.skull1);
startCountDownTimer();
}
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 100, 20);
}
public void stoptimertask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
goDownBoxAndSkull();
}
});
}
};
}
//FUNCTIONS FOR BOX AND SKULL TO GO DOWN
public void goDownBoxAndSkull() {
//SKULL FUNCTIONS
//SCORE CHECK TO SET NEW SPEED
if (scoreCount >= 0 && scoreCount < 5) {
skullY += 5;
if (skullY > frame.getHeight()) {
newXposition = (int) box.getX();
skullY = -150.0f;
skullX = newXposition;
}
if (skullX < 0) {
skullX = 0;
}
skull.setX(skullX);
skull.setY(skullY);
}
if (scoreCount >= 5 && scoreCount < 1000) {
skull1.setVisibility(View.VISIBLE);
skullY += 10;
skullY1 += 10;
if (skullY > frame.getHeight()) {
newXposition = (int) box.getX();
skullY = -150.0f;
skullX = newXposition;
}
if (skullX < 0) {
skullX = 0;
}
if (skullY1 > frame.getHeight()) {
newXposition = (int) box.getX();
skullY1 = -150.0f;
skullX1 = newXposition;
}
if (skullX1 < 0) {
skullX1 = 0;
}
skull.setX(skullX);
skull.setY(skullY);
skull1.setX(skullX1);
skull1.setY(skullY1);
}
//CHECK IF BOX PASSED SKULL TO ADD SCORE
if (skull.getY() == box.getY()) {
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
if (skull1.getY() == box.getY()) {
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
//BOX FUNCTIONS
if (tapped == true) {
boxX -= 5;
} else {
boxX += 5;
}
//BOX GOING TO LEFT
if (boxX < 0) {
boxX = 0;
}
//BOX GOING TO RIGHT
if (boxX > frame.getWidth() - box.getWidth())
{
boxX = frame.getWidth() - box.getWidth();
}
box.setX(boxX);
}
//COUNTDOWN TIMER TO START
public void startCountDownTimer()
{
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
txtTimer.setText("GAME WILL START IN \n" + millisUntilFinished / 1000);
}
public void onFinish() {
startTimer();
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
txtTimer.setVisibility(View.GONE);
txtScore.setVisibility(View.VISIBLE);
box.setVisibility(View.VISIBLE);
skull.setVisibility(View.VISIBLE);
//SET RANDOM START POSITION FOR SKULL
newXposition = r.nextInt(frame.getWidth());
skullX = newXposition;
skull.setX(skullX);
//SET BOX IN THE MIDDLE
boxX = frame.getWidth() / 2;
boxY = frame.getHeight() / 2;
box.setX(boxX);
box.setY(boxY);
}
}.start();
}
最佳答案
代码很宽,但是如果我理解您可以在此处添加分数:
if (skull.getY() == box.getY()) {
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
if (skull1.getY() == box.getY()) {
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
在第一种情况下,您使用增量,以便每个都是盒位置的倍数,因此满足
==
条件。在第二种情况下,它们不是倍数,这可能导致条件从不满足。
只需执行以下操作即可解决:
if (skull.getY() >= box.getY() && skull.scoreCounted == false) {
skull.scoreCounted = true;
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
if (skull1.getY() >= box.getY() && skull1.scoreCounted == false) {
skull.scoreCounted = true;
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
编辑:假设您有一个
skull.scoreCounted
公共布尔标志,您可以使用它来知道是否已考虑该框的点。代码有点不清楚,我不能说比较应该是
>=
还是=<
。顺便说一句,我不建议以这种方式创建游戏,因为它效率低下,不舒服且相当糟糕。
如果您对Android游戏开发感兴趣,请查看一些特殊框架,例如LibGDX