如何在我的播放状态课程中更新我的游戏分数

时间:2017-12-22 06:21:43

标签: java android libgdx refresh

我正在开发小型动画游戏,当我触摸物体时会杀死一些物体。

因为我在杀死他们时得分,但我不知道在我的游戏班上显示我的最新分数。

这是我的班级:

 public class PlayState extends State implements Input.TextInputListener {
        private static int FREQUENCY=100;
        boolean alive;
        Texture mBackground,mScore;
        List<Bug> bugs;
        int x,type,count,score;
        int xScale,yScale;

        public PlayState(StateManager manager) {
            super(manager);
            mBackground=new Texture("bg_game1.jpg");
            mScore=new Texture("score.jpg");
            bugs=new ArrayList<Bug>();
            alive=true;
            count=FREQUENCY;
            score=0;
            xScale=AntSmasher.WIDTH/AntSmasher.VIRTUALWIDTH;
            yScale=AntSmasher.HEIGHT/AntSmasher.VIRTUALHEIGHT;
        }

        @Override
        public void handleInput() {
            if(Gdx.input.justTouched()){
                if(alive) {
                    for (Bug ref:bugs) {
                        if(ref.getLife()){
                            TextureRegion image = ref.getRegion();
                            int
                                    xTouch = Gdx.input.getX(),
                                    yTouch = Gdx.input.getY();
                            int
                                    xMin = (int) ref.getPosition().x,
                                    xMax = xMin + image.getRegionWidth()*xScale,
                                    yMin = AntSmasher.HEIGHT - ((int) ref.getPosition().y) - image.getRegionHeight()*yScale,
                                    yMax = yMin + image.getRegionHeight()*yScale;
                            if (xTouch <= xMax && xTouch >= xMin &&
                                    yTouch <= yMax && yTouch >= yMin) {
                                FREQUENCY*=1.0;
                                AntSmasher.YSPEED*=1;
                                ref.die();
                                if(ref instanceof BlackAnt)
                                    score++;
                                else if(ref instanceof BrownAnt)
                                    score+=2;
                                else
                                    score+=3;
                            }
                        }
                    }
                }
            }
        }

        @Override
        public void dispose() {
            mBackground.dispose();
            mScore.dispose();
            for (Bug ref2:bugs)
                ref2.dispose();
        }

        @Override
        public void update(float dt) {
            handleInput();
            if(alive) {
                if (count++ >= FREQUENCY) {
                    x = (int) (Math.random() * (AntSmasher.WIDTH-100*xScale) + 1);
                    type = (int) (Math.random() * 3);
                    if (type == 0) {
                        BlackAnt bug = new BlackAnt(x);
                        bugs.add(bug);
                    } else if (type == 1) {
                        BrownAnt bug = new BrownAnt(x);
                        bugs.add(bug);
                    } else {
                        Scorpion bug = new Scorpion(x);
                        bugs.add(bug);
                    }
                    count = 0;
                }
                for (Bug ref : bugs){
                    ref.update(dt);
                    if(ref.getPosition().y<=0) {
                        alive = false;
                        Gdx.input.getTextInput(this, "Game Over", "", "Score is : "+String.valueOf(score));
                        AntSmasher.YSPEED=-400;
                        FREQUENCY=100;
                        manager.set(new MenuState(manager));
                        break;
                    }
                }
            }
        }

            @Override
    public void render(SpriteBatch sb) {

        sb.begin();
        sb.draw(mBackground,0,0,AntSmasher.WIDTH,AntSmasher.HEIGHT);
        BitmapFont font = new BitmapFont();
        font.draw(sb,"Score :"+ String.valueOf(score), 500, Align.topLeft);
        font.setColor(Color.WHITE);
        float ratio=(float)AntSmasher.WIDTH/(float)mScore.getWidth();
        sb.draw(mScore,
                (AntSmasher.WIDTH-mScore.getWidth()*ratio)/2,
               AntSmasher.HEIGHT-mScore.getHeight()*ratio-50*AntSmasher.HEIGHT/AntSmasher.VIRTUALHEIGHT,
                mScore.getWidth()*ratio,
                mScore.getHeight()*ratio);
        for(Bug ref:bugs) {
            if(ref.getLife())
                sb.draw(ref.getRegion(),
                        ref.getPosition().x,
                        ref.getPosition().y,
                        ref.getRegion().getRegionWidth()*xScale,
                        ref.getRegion().getRegionHeight()*yScale);
            else
                sb.draw(ref.getImage(),
                        ref.getPosition().x,
                        ref.getPosition().y,
                        ref.getImage().getWidth()*xScale,
                        ref.getImage().getHeight()*yScale);
        }

        sb.end();
    }

      @Override
        public void input(String text) {
            AntSmasher.firebase.addToFirebase(text,score);
            //AntSmasher.firebase.addToFirebase("Okay",score);
            score=0;
        }

        @Override
        public void canceled() {
            AntSmasher.firebase.addToFirebase("Anónimo",score);
            score=0;
        }
    }

请帮助我,因为我错过了在播放状态屏幕上更新分数的代码。

1 个答案:

答案 0 :(得分:0)

您需要在公共无效渲染中添加您的得分代码绘图。像这样的东西

BitmapFont font = new BitmapFont();
font.draw(sb, String.valueOf(score), 200, 200);

启动字体更好地转移到构造函数

还尝试将这两个字符串添加到render方法的开头

Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);