我需要做的是,当点其“示例10”时,球的速度上升,当点其“示例20”时,球的速度再次上升,等等。我不知道我是否在球中这样做类或在GameScreen类中,我不知道该怎么做。谢谢你的帮助!。

public class Ball {

    //THE SPEED OF THE BALL
    private static final float SPEED=1100;

    //THE POINTS IS
    puntuacion =0;

    //AND THE METHOD WHEN THE POINTS IS INCREAMENTING
    private void updatePuntuacion(){
        if(ball.getBordes().overlaps(Lpaddle.getBordes())) {
            puntuacion = puntuacion + 1;
            if(puntuacion > puntuacionMaxima)
            puntuacionMaxima = puntuacion;
        }

        if(ball.getBordes().x <= 0)
            sonidoex.play();

        if(ball.getBordes().x <= 0)
            puntuacion =0;

        if(ball.getBordes().x <=0)
            Gdx.input.vibrate(1000);

        if(ball.getBordes().x <=0)
            Screens.juego.setScreen(Screens.MAINSCREEN);

        ball.comprobarPosicionBola();
    }
}


编辑:

这是我的GameScreen类。

public class GameScreen extends AbstractScreen {

    private SpriteBatch batch;
    private Texture texture;
    private Paddle Lpaddle, Rpaddle;
    private Ball ball;
    private BitmapFont font;
    private int puntuacion, puntuacionMaxima;
    private Preferences preferencias;
    private Music music;
    private Sound sonidoex;

    public GameScreen(Main main) {
        super(main);
            preferencias = Gdx.app.getPreferences("PuntuacionAppPoints");
            puntuacionMaxima = preferencias.getInteger("puntuacionMaxima");
            music =Gdx.audio.newMusic(Gdx.files.internal("bgmusic.mp3"));
            music.play();
            music.setVolume((float) 0.3);
            music.setLooping(true);
            sonidoex = Gdx.audio.newSound(Gdx.files.internal("explosion5.wav"));
    }

    public void show(){
        batch = main.getBatch();
        texture = new Texture(Gdx.files.internal("spacebg.png"));

        Texture texturaBola = new Texture(Gdx.files.internal("bola.png"));
        ball = new Ball(Gdx.graphics.getWidth() / 2 - texturaBola.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texturaBola.getHeight() / 2);
        Texture texturaPala= new Texture(Gdx.files.internal("pala.png"));
        Lpaddle = new LeftPaddle(80, Gdx.graphics.getHeight()/2 -texturaPala.getHeight() /2);
        Rpaddle = new RightPaddle(Gdx.graphics.getWidth() -100, Gdx.graphics.getHeight()/2 - texturaPala.getHeight() /2, ball);
        font = new BitmapFont(Gdx.files.internal("pointsfont.tf.fnt"), Gdx.files.internal("pointsfont.tf.png"), false);
        puntuacion = 0;
    }

    public void render(float delta){
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        updatePuntuacion();
        Lpaddle.update();
        Rpaddle.update();
        ball.update(Lpaddle, Rpaddle);
        batch.begin();
        batch.draw(texture, 0, 0,texture.getWidth(), texture.getHeight());
        ball.draw(batch);
        Lpaddle.draw(batch);
        Rpaddle.draw(batch);
        font.draw(batch, "Points: " + Integer.toString(puntuacion), Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
        font.draw(batch, "High score: " + Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
        batch.end();
    }

    private void updatePuntuacion(){
        if(ball.getBordes().overlaps(Lpaddle.getBordes())) {
            puntuacion = puntuacion + 1;
            if(puntuacion > puntuacionMaxima)
            puntuacionMaxima = puntuacion;
        }

        if(ball.getBordes().x <= 0)
            sonidoex.play();

        if(ball.getBordes().x <= 0)
            puntuacion =0;

        if(ball.getBordes().x <=0)
            Gdx.input.vibrate(1000);

        if (puntuacion % 10 == 0){
             SPEED = 200;
        }

        if(ball.getBordes().x <=0)
            Screens.juego.setScreen(Screens.MAINSCREEN);

        ball.comprobarPosicionBola();
    }

    public void hide(){
        font.dispose();
        texture.dispose();
    }

    @Override
    public void dispose(){
        preferencias.putInteger("puntuacionMaxima", puntuacionMaxima);
        preferencias.flush();
    }

    public void resize(int width, int height){
        float widthImage = texture.getWidth();
        float heightImage = texture.getHeight();
        float r = heightImage / widthImage;
        if(heightImage > height) {
            heightImage = height;
            widthImage = heightImage / r;
        }

        if(widthImage > width) {
            widthImage = width;
            heightImage = widthImage * r;
        }
    }
}

最佳答案

我需要50声望才能发表评论,所以我会发布答案,并尝试使它尽可能有帮助。

首先,我不太确定您的问题,但是据我了解,您想在puntuacion达到某些值(10、20等)时提高球的速度。

您要做的第一件事是从final变量中删除SPEED,因为final原语只能设置一次,因此您以后便无法对其进行更改。

至于更新速度,如果要以特定的punctuacion值进行,则只需执行

if(punctuacion == 10 || punctuacion == 17) { // etc
    speed += x; // x being the value you want to increase the speed by
}


另外,例如,如果您想每10个增量更新一次速度,则可以

if (punctuacion % 10 == 0){
    speed += x;
}


请注意,对于第二个示例,当punctuacion0时,if语句将返回true,因此,如果必须解决此问题。

至于放置此代码的位置,它实际上取决于您的类,并且您肯定没有为我提供足够的代码来帮助您,但是我最好的猜测是在增加punctuacion之后将其正确放置,因为我假设您希望在punctuacion更新之后立即执行对球的速度更新检查。

关于java - libGDX快速咨询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26229446/

10-10 01:41