libGDX与速度协商 [英] libGDX consult with speed

查看:297
本文介绍了libGDX与速度协商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的是,当点的例10球加薪的速度,当其要点例如20球的速度再次引起,等我不知道如果我这样做在Ball类或GameScreen类,我不知道如何做到这一点。感谢您的帮助!

 公共类球{    //球的速度
    私有静态最终浮动SPEED = 1100;    //点​​IS
    分数= 0;    //,并且该方法时,点数为INCREAMENTING
    私人无效updatePuntuacion(){
        如果(ball.getBordes()。重叠(Lpaddle.getBordes())){
            分数=分数+ 1;
            如果(分数> puntuacionMaxima)
            puntuacionMaxima =分数;
        }        如果(ball.getBordes()X​​ - 下; = 0)
            sonidoex.play();        如果(ball.getBordes()X​​ - 下; = 0)
            分数= 0;        如果(ball.getBordes()X​​ - 下; = 0)
            Gdx.input.vibrate(1000);        如果(ball.getBordes()X​​ - 下; = 0)
            Screens.juego.setScreen(Screens.MAINSCREEN);        ball.comprobarPosicionBola();
    }
}

编辑:

这是我的GameScreen类。

 公共类GameScreen扩展AbstractScreen {    私人SpriteBatch批次;
    私人纹理质感;
    私人桨Lpaddle,Rpaddle;
    私人球球;
    私人BitmapFont字体;
    私人诠释分数,puntuacionMaxima;
    私人preferences preferencias;
    私人音乐音乐;
    私人声音sonidoex;    公共GameScreen(主主){
        超(主);
            preferencias = Gdx.app.get preferences(PuntuacionAppPoints);
            puntuacionMaxima = preferencias.getInteger(puntuacionMaxima);
            音乐= Gdx.audio.newMusic(Gdx.files.internal(bgmusic.mp3));
            music.play();
            music.setVolume((浮点)0.3);
            music.setLooping(真);
            sonidoex = Gdx.audio.newSound(Gdx.files.internal(explosion5.wav));
    }    公共无效显示(){
        批次= main.getBatch();
        纹理=新的纹理(Gdx.files.internal(spacebg.png));        纹理texturaBola =新的纹理(Gdx.files.internal(bola.png));
        球=新球(Gdx.graphics.getWidth()/ 2 - texturaBola.getWidth()/ 2,Gdx.graphics.getHeight()/ 2 - texturaBola.getHeight()/ 2);
        纹理texturaPala =新的纹理(Gdx.files.internal(pala.png));
        Lpaddle =新LeftPaddle(80,Gdx.graphics.getHeight()/ 2 -texturaPala.getHeight()/ 2);
        Rpaddle =新RightPaddle(Gdx.graphics.getWidth()-100,Gdx.graphics.getHeight()/ 2 - texturaPala.getHeight()/ 2,球);
        字体=新BitmapFont(Gdx.files.internal(pointsfont.tf.fnt),Gdx.files.internal(pointsfont.tf.png),FALSE);
        分数= 0;
    }    公共无效渲染(浮动三角洲){
        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(纹理,0,0,texture.getWidth(),texture.getHeight());
        ball.draw(批);
        Lpaddle.draw(批);
        Rpaddle.draw(批);
        font.draw(批,点+ Integer.toString(分数),Gdx.graphics.getWidth()/ 4,Gdx.graphics.getHeight() - 5);
        font.draw(批次,高得分:+ Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth()/ 4,Gdx.graphics.getHeight() - 5);
        batch.end();
    }    私人无效updatePuntuacion(){
        如果(ball.getBordes()。重叠(Lpaddle.getBordes())){
            分数=分数+ 1;
            如果(分数> puntuacionMaxima)
            puntuacionMaxima =分数;
        }        如果(ball.getBordes()X​​ - 下; = 0)
            sonidoex.play();        如果(ball.getBordes()X​​ - 下; = 0)
            分数= 0;        如果(ball.getBordes()X​​ - 下; = 0)
            Gdx.input.vibrate(1000);        如果(分数10%== 0){
             SPEED = 200;
        }        如果(ball.getBordes()X​​ - 下; = 0)
            Screens.juego.setScreen(Screens.MAINSCREEN);        ball.comprobarPosicionBola();
    }    公共无效的hide(){
        font.dispose();
        texture.dispose();
    }    @覆盖
    公共无效的Dispose(){
        preferencias.putInteger(puntuacionMaxima,puntuacionMaxima);
        preferencias.flush();
    }    公共无效调整大小(INT宽度,高度INT){
        浮widthImage = texture.getWidth();
        浮heightImage = texture.getHeight();
        浮动R = heightImage / widthImage;
        如果(heightImage>高度){
            heightImage =高度;
            widthImage = heightImage / R;
        }        如果(widthImage>宽度){
            widthImage =宽度;
            heightImage = widthImage * R;
        }
    }
}


解决方案

我需要50声誉发表评论这样反而我会发布一个答案,并努力使之尽可能的帮助。

首先我不太清楚你的问题,但是从我个人理解,你想增加你的球的速度,当分数达到一定值( 10,20,等等)。

你必须做的第一件事就是删除最后速度变量,因为最后基本只能设置一次,因此,从后来改变它拒绝你。

至于更新的速度,如果你想在 punctuacion 的具体值做到这一点,那么简单地做

 如果(punctuacion == || 10 == punctuacion 17){//等
    速度+ = X; // x是要通过增加速度值
}

另外,如果您想更新的速度每10个增量例如,你可以做

 如果(punctuacion%10 == 0){
    速度+ = X;
}

请注意,第二个例子,当 punctuacion 0 ,if语句将返回真正,所以解决,如果你不得不这样做。

至于在哪里把这个code,这真的取决于你的课,你一定没有张贴足够code对我来说,能够帮你,但我最好的猜测将是把它放在你增加之后 punctuacion ,因为我假设你想成为后 punctuacion 更新。

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.

这篇关于libGDX与速度协商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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