android - 改变物体掉落的速度

标签 android

我正在创建一个 Android 游戏,但在增加分数时遇到了问题。

所以有 2 个物体,一个放在屏幕中间,一个物体落到屏幕底部。

当掉落的物体经过屏幕中间的物体时,它会添加一个点。

当我将下落物体的速度设置为 skullY += 5skullY += 10 时,一切正常并且分数被添加到分数中,但是如果我将速度更改为 skullY += 7skullY += 12 这样速度会更快,并且在通过时不会添加分数。

这是我目前遇到的问题。

这是我的 Activity 代码:

 //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公共(public) bool 标志,您可以使用它来了解是否已考虑该框的点。

代码有点不清楚,我不能说比较应该是 >==< .

顺便说一下,我不建议以这种方式创建游戏,因为我认为它效率低下且不舒服,而且非常糟糕。

如果您对 Android 游戏开发感兴趣,请查看一些临时框架,例如 LibGDX

关于android - 改变物体掉落的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265868/

相关文章:

安卓全息主题背景

Android TabHost 导致运行时错误(NullPointerException)

android - 带jar输出的自定义gradle项目

java - 如何将增量发票编号放入DatabaseReference

android - list 合并失败 : Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported`

java - 同时使用大写和小写搜索

java - UDP 数据包在 Java 中接收正常,但在 Python 中损坏

android - 机器人 "INTERNET permission is required"

php - 使用 PHP 从浏览器通过 Android 摄像头捕获图像

android - 错误 : ToolBar theme and Dark theme