2014-10-07 77 views
0

我需要做的是,当它的“例子10”指出球的速度提高,当它的“例子20”球的速度再次提高时等我不知道如果我在Ball类或GameScreen类中这样做,我不知道该怎么做。谢谢你的帮助!。libGDX与速度咨询

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

回答

1

我需要50个声望才能发表评论,所以我会发布一个答案,并尽量使其尽可能地有帮助。

首先我不太确定你的问题,但是根据我的理解,当puntuacion达到某个值(10,20等)时,你想增加球的速度。

您必须做的第一件事是从您的SPEED变量中删除final,因为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更新后立即执行球的速度更新检查。

+0

我做什么,你在第二个例子说,似乎工作得很好,唯一的问题是,当我把这个: IF(分数10%== 0){ \t \t SPEED = 200; \t \t} 它在另一个类,并说“SPEED”不能是一个变量。 – JAnoob92 2014-10-07 07:32:59

+0

这里是GAMESCREEN。 – JAnoob92 2014-10-07 07:34:48

+0

在您的'Ball'类中创建一个公共setter:'setSpeed(float speed)',并在您的GameScreen类中使用它来更改'SPEED'的值。另外,考虑除去速度变量的静态修饰符,除非你绝对需要静态'' – Tiberiu 2014-10-07 16:15:37

0

这是我的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; 
     } 


    } 

} 
+0

我错过了+符号,但无论如何,代码不起作用。 – JAnoob92 2014-10-07 07:52:06

+0

@ JAnooob92你应该更新你的答案,而不是发布更新作为答案! – Springrbua 2014-10-07 08:22:46