在屏幕上遇到一次激活特定触摸事件的特定变量的问题

时间:2014-12-18 20:13:55

标签: android libgdx

所以我正在制作一个游戏,用户将四只手放在屏幕上,在屏幕上的那些触摸上产生四个圆圈,并在这些上方指示偏移。指示器在特定圆圈上熄灭,并且用户需要触摸以增加其分数并返回到他们从其抬起手指的圆圈。我有四个变量来处理每个手指的得分,原因是稍后使用它们作为统计数据,游戏的递增部分按原样运行。但是,我的问题是在递减时,有时每个圆圈的分数会正确递减,但大多数时候它没有正确地进行。

以下是我目前遇到问题的代码段:

public void scoreDeducter(OrthographicCamera cam, float elapsedTime,
        int currentIndex) {

    //the for loop iterating over the 4 circles on the screen
    for (int placerIndex = 0; placerIndex < placerCoordinates.length; placerIndex++) {
        Vector3 placerVector = new Vector3(
                placerCoordinates[placerIndex].x,
                placerCoordinates[placerIndex].y, 0);

        cam.unproject(placerVector);
        //the currentIndex is where the indicator has gone green and so to avoid
        //the score to decrement for that pointer we don't execute the inner loop
        //as we would like to increment score on touch up.
        if (currentIndex != placerIndex) {
            //goes through the various touch coordinates currently on the screen
            for (int pointer = 0; pointer < 5; pointer++) {

                Vector3 touchVector = new Vector3(Gdx.input.getX(pointer),
                        Gdx.input.getY(pointer), 0);
                //we unproject the pointer's coordinates x and y
                cam.unproject(touchVector);

                //this is the distance between the placer (circle) and the touch on the screen
                //for the pointer
                float distance = placerVector.dst(touchVector.x,
                        touchVector.y, 0);
                int radius = 20;

                //checks whether this pointer has been touched
                boolean isCurrentTouched = Gdx.input.isTouched(pointer);
                //boolean isCurrentTouched = Gdx.input.isTouched();

                //if the distance is less than the circle's radius and is touched
                //then we know that the person hasn't made an error, therefore we skip the
                //pointer to 5, without making any further checks whether it is touched or not.
                if (distance < radius && isCurrentTouched) {

                    pointer = 5;
                }

                //otherwise should the pointer's value be 4 and the if statement
                //condition above fails we decrement the score off a circle as they
                //lifted their finger on the placer where the indicator hasn't gone green
                else if (pointer == 4) {
                    if (elapsedTime >= 5.0f && !isCurrentTouched) {

                        if (placerIndex == 0 && f1Score != 0) {
                            f1Score = f1Score - 1;
                        }
                        if (placerIndex == 1&& f2Score != 0) {

                            f2Score = f2Score - 1;
                        }
                        if (placerIndex == 2 && f3Score != 0) {
                            f3Score = f3Score - 1;
                        }
                        if (placerIndex == 3 && f4Score != 0) {
                            f4Score = f4Score - 1;
                        }
                    }
                }
            }
        }
    }
}

然后在我的GameRenderer类中以render方法呈现:

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    System.out.println("f1 score: " + world.getFingerPlacer().getF1Score()
            + " f2 score: " + world.getFingerPlacer().getF2Score()
            + " f3 score: " + world.getFingerPlacer().getF3Score()
            + " f4 score: " + world.getFingerPlacer().getF4Score());

    // System.out.println("f1 Errors: " + f1Errors + " f2 Errors: " +
    // f2Errors + " f3Errors: " + f3Errors + " f4Errors: " + f4Errors);

    // System.out.println("The score: "+
    // world.getFingerPlacer().getTotalScore());

    if ((!world.getFingerPlacer().arePlacersFilled() && !world
            .getIndicator().areIndicatorsFilled()) && Gdx.input.isTouched()) {
        world.getFingerPlacer().addFingerPlacers();
        world.getIndicator().generateCoords();
    }

    if (world.getFingerPlacer().arePlacersFilled()
            && world.getIndicator().areIndicatorsFilled()) {

        // we sort the lists so that the indicators match with the
        // corresponding placers
        world.bubblesort(world.getFingerPlacer().getPlacerCoords());
        world.bubblesort(world.getIndicator().getIndicatorCoords());

        // the rendering of the placers take place
        renderFingerPlacers();
        renderIndicators();
        renderIndicatorColour(delta);
        timeKeeper(delta);

    }
}

我的scoreDeducter()方法在renderIndicatorColour方法中被调用:

public void renderIndicatorColour(float delta) {

    shapeRenderer.begin(ShapeType.Filled);

    shapeRenderer.setColor(50 / 130f, 210 / 130f, 56 / 130f, 1f);

    Vector3 touchVector = new Vector3(world.getIndicator()
            .getIndicatorCoords()[index].x, world.getIndicator()
            .getIndicatorCoords()[index].y, 0);

    camera.unproject(touchVector);

    shapeRenderer.rect(touchVector.x, touchVector.y, 20, 10);
    shapeRenderer.end();

    elapsedTime += delta;

    justTouchedUp = Gdx.input.getInputProcessor().touchUp(
            (int) world.getFingerPlacer().getPlacerCoords()[index].x,
            (int) world.getFingerPlacer().getPlacerCoords()[index].y, 0, 0);

    if (justTouchedUp) {

        wasTouchedUp = true;
    }

    world.getFingerPlacer().scoreDeducter(camera, elapsedTime, index);

}

提前谢谢。

0 个答案:

没有答案
相关问题