布尔值明确为true时返回false [英] Boolean returns false when it is clearly true

查看:102
本文介绍了布尔值明确为true时返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序有一个超级简单的多人游戏.当玩家玩的游戏结束时,布尔值 gameFinished 变为true.游戏中发生的大多数事情都首先检查以确保 gameFinished 等于false,这就是我的问题所在.游戏结束后,我的布尔值显然设置为true,日志返回 false ,因此游戏不断循环播放.

So I have a super simple multiplayer game for my app. And when the game the players play ends, the Boolean gameFinished becomes true. Most of the things that happen in the game first check to make sure gameFinished is equal to false, and that is where my problem comes in. After the game is finished, and my Boolean is clearly set to true, the Log returns false so the game keeps looping.

这是获取游戏结果的方法

So here is the method that gets the results of the game

    private void getResults() {
    HomePage.getCurrentGameID(new HomePage.CallbackID() {
        @Override
        public void onSuccess(final String currentGameID) {
                games.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
                        HomePage.getUserPosition(new HomePage.CallbackPosition() {
                            @Override
                            public void onSuccess(int position) {
                                if (dataSnapshot.child(currentGameID).child("gameFinished").getValue().equals(false)) {
                                    if (position == 1) {
                                        if (youWin()) {
                                            getWinner(FirebaseAuth.getInstance().getUid(), wager);
                                            Log.i("FlipCoin", dataSnapshot.child(currentGameID).child("gameFinished").getValue().toString());
                                        } else {
                                            getLoser(FirebaseAuth.getInstance().getUid(), wager);

                                        }
                                    }
                                    endGame();
                                }
                            }
                        });
                    }

抱歉,该方法中有很多草率的代码.

Sorry, there is a lot of sloppy code in that method.

但是我在那里排队, if(dataSnapshot.child(currentGameID).child("gameFinished").getValue().equals(false)){

这将检查游戏是否结束,如果尚未结束,则会在游戏结束时找到赢家或输家.

That checks to see if the game is finished, and if it is not finished then the winner or loser will be found at the end of the game.

但是,一旦游戏结束,并且使用此方法找到了获胜者和失败者,该方法就会循环并一遍又一遍地运行,因为它认为 gameFinished 等于 false .

However, once the game is over, and the winner and loser are found using this method, the method loops and continues to run over and over and over because it thinks that gameFinished is equal to false.

我有一个 endGame()方法发生在此 getResults()方法的结尾

I have an endGame() method that happens at the end of this getResults() method

    private void endGame() { HomePage.getCurrentGameID(new HomePage.CallbackID() {
        @Override
        public void onSuccess(final String currentGameID) {
            games.child(currentGameID).child("gameFinished").setValue(true);
            games.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    Log.i("FlipCoin", dataSnapshot.child(currentGameID).child("gameFinished").getValue().toString());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    });
}

在该方法中,我使用此行将 gameFinished 设置为true games.child(currentGameID).child("gameFinished").setValue(true);

And in that method I clearly set gameFinished to true using this line games.child(currentGameID).child("gameFinished").setValue(true);

它反映在我的实时数据库中,就像这样

And it is reflected in my realtime database that looks like this

从视觉上讲,这是事实.而且我的endgame()方法中还有一条log语句,该语句返回 true ,因此根据该log语句,布尔值应为true

So visually, it is true. And I also have a log statement in my endgame() method that returns true, so according to the log statement the Boolean should be true

但是我的 getResults()方法中也有log语句,即使布尔值很明显,该日志也返回false

But I also have log statements in my getResults() method, and that log returns false, even though the Boolean is clearly true

有人可以帮助我解决这个奇怪的问题吗?

Can anyone help me with this odd problem?

这显示了logcat如何反复地从false切换为true,这毫无意义

This shows how the logcat is switching from false to true over and over which doesn't make any sense

推荐答案

您正在将true值与false值进行比较.因此它返回false.像这样:

You are comparing a true value with a false. so it returns false. like this:

true.equals(false)

返回false.

将您的代码更改为以下代码行:

Change your code to this line of code:

if(dataSnapshot.child(currentGameID).child("gameFinished").getValue().equals(false))

对此:

if((Boolean)dataSnapshot.child(currentGameID).child("gameFinished").getValue()) 

这篇关于布尔值明确为true时返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆