libgdx change background after three attempts

最后都变了- 提交于 2019-12-24 21:40:55

问题


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.

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

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

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.


回答1:


Well, Lets say you have a Sprite that holds the whole texture.

Sprite timeOfDay = new Sprite(texture);

Then by simply tweaking your changeBG(int x) method. You can appropriately set the sprite to the Region you want.

public void changeBG(int x){
    if(x < 5) //Assuming x is the time
        timeOfDay.setRegion(DAY);
    else
        timeOfDay.setRegion(NIGHT);
}

and then in your draw() method after you set the batch up

timeOfDay.draw(batch);

I hope this helps.

[Update]

No need to draw DAY and NIGHT in your render method. Time of Day holds the DAY and NIGHT textures. When you call

timeOfDay.draw(batch); 

it renders the setRegion.

Your render should look something like this....

public void draw(){
    batcher.begin();
    timeOfDay.draw(batcher);
    batcher.end();
}



回答2:


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

Then you create a Sprite:

Sprite sprite = new Sprite(DAY);

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

or

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

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


来源:https://stackoverflow.com/questions/24057817/libgdx-change-background-after-three-attempts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!