经过三次尝试libgdx改变背景 [英] libgdx change background after three attempts

查看:170
本文介绍了经过三次尝试libgdx改变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会再制定我的previous问题。我想从我的一天游戏改变夜晚的背景。这应的两个或三次尝试后进行玩游戏。
我有一个纹理白天和夜间不同textureRegions。
任何帮助是非常AP preciated。
我用eclipse工作。

I'll formulate my previous question again. I want the background of my game change from day to night. This should be done after two or three tries to play the game. I have óne texture with different textureRegions for the day and the night. Any help is very much appreciated. I work with eclipse.

这是什么在我的AssetLoader.java

this is what a have in my AssetLoader.java

DAY= new TextureRegion(texture, 0, 0, 287, 512);
DAY.flip(false, true);
NIGHT= new TextureRegion(texture, 291, 0, 287,512);
NIGHT.flip(false, true);

这是什么在我的GameRenderer.java

This is what a have in my GameRenderer.java

public void changeBG(int x){
if(x < 3){
drawDAY();
} 
else if (x < 6)
{
drawNIGHT();
}
}
private void drawNIGHT() {
    // TODO Auto-generated method stub
}
 private void drawDAY() {
   // TODO Auto-generated method stub}
}

这是我在我的GameWorld.java

This is what i have in my GameWorld.java

  public void update(float delta) {
   runTime += delta;

   switch (currentState) {
   case READY:
   case MENU:
         updateReady(delta);
         break;
   case RUNNING:
         updateRunning(delta);
         break;
   default:
      break;
   }
     }
public boolean isReady() {
     return currentState == GameState.READY;
  }

我希望这是足够的信息。
问候。

I hope this is enough information. Greetings.

推荐答案

正如我在previous回答说部分,则需要从头开始做的是pretty多少以下内容:

As I've partially said in the previous answer, what you need to do from scratch is pretty much the following:

DAY= new TextureRegion(texture, 0, 0, 287, 512);
DAY.flip(false, true);
NIGHT= new TextureRegion(texture, 291, 0, 287,512);
NIGHT.flip(false, true);

然后创建一个Sprite:

Then you create a Sprite:

Sprite sprite = new Sprite(DAY);

我猜你将它设置为屏幕的大小,这取决于你是否使用Scene2d或正交变换或只是去它显然与屏幕坐标:

I'm guessing you will set it to the size of the screen, which depends on whether you use Scene2d or an Orthogonal transform or just go at it plainly with screen coordinates:

sprite.setSize(Gdx.graphics.width, Gdx.graphics.height);

sprite.setSize(virtualWidth, virtualHeight); //in new version of LibGDX this is standard 640x480

和之后,基于游戏的逻辑,你会想改变TextureRegion。要存储你有多少次重试,您需要使用preferences:

And afterwards, based on the logic of the game, you will want to change the TextureRegion. To store how many times you've retried, you need to use the Preferences:

private static Preferences preferences; 

@Override
public void create()
{
    preferences = Gdx.app.getPreferences(Resources.preferencesName);
    ...

public static Preferences getPreferences()
{
    return preferences;
}

这当比赛结束后,你做到以下几点:

After which when the game ended, you do the following:

您添加的号码作为一个游戏下面到改变的尝试次数:

Where you add the number as the following at a game over to change the number of tries:

    int currentTries = MyGame.getPreferences().getInt("numberOfTries");
    currentTries++;
    currentTries %= 6;
    MyGame.getPreferences().putInt("numberOfTries", currentTries);
    MyGame.getPreferences().flush();
    changeBG(currentTries);

然后你改变当前纹理区域:

And then you change the current texture region:

 public void changeBG(int x){
    if(x < 3) {
        sprite.setRegion(DAY);
    }
    else if (x < 6) {
        sprite.setRegion(NIGHT);
    }
 }

这篇关于经过三次尝试libgdx改变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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