简体   繁体   中英

libGDX consult with speed

What i need to do is, when the points its "example 10" the speed of the ball raise, when the points its "example 20" the speed of the ball raises again, etc. I dont know if i do that in the Ball class or in the GameScreen class and i dont know how to do it. Thanks for your help!.

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();
    }
}

EDIT:

This is my GameScreen class.

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;
        }
    }
}

I need 50 reputation to comment so instead I'll post an answer and try to make it as helpful as possible.

First of all I'm not too sure of your question, but from what I understand, you want to increase the speed of your ball when puntuacion reaches certain values (10, 20, etc).

The first thing you have to do is remove the final from your SPEED variable, since a final primitive can only be set once, thus denying you from changing it later on.

As for updating the speed, if you want to do it at specific values of punctuacion , then simply do

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

Alternatively, if you want to update the speed every 10 increments for example, you could do

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

Do note that for this second example, when punctuacion is 0 , the if statement will return true , so work around that if you have to.

As for where to put this code, it really depends on your classes and you definitely didn't post enough code for me to be able to help you with that, but my best guess would be to put it right after you increment punctuacion , since I'm assuming you want the ball's speed update check to be performed immediately after punctuacion updates.

This is my GameScreen class.

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;
        }


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM