我正在创建一款Android游戏,在增加分数时遇到了问题。
所以有2个物体,一个放在屏幕中间,一个物体落到屏幕底部。
当落下的物体通过屏幕中间的物体时,它会增加一个点。
当我将坠落物体的速度设置为头骨+ + 5 或头骨+ + 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();
}
答案 0 :(得分:1)
代码很宽,但如果我理解你在这里添加一个分数:
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