【问题标题】:libgdx change background after three attempts三次尝试后 libgdx 更改背景
【发布时间】:2014-06-05 10:31:34
【问题描述】:

我将再次提出我之前的问题。我希望我的游戏背景从白天变为夜晚。这应该在两次或三次尝试玩游戏后完成。 我有一个纹理,白天和晚上都有不同的纹理区域。 很感谢任何形式的帮助。 我与日食一起工作。

这就是我的 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 中的内容

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 中的内容

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

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

【问题讨论】:

    标签: android eclipse libgdx


    【解决方案1】:

    好吧,假设您有一个拥有整个纹理的 Sprite。

    Sprite timeOfDay = new Sprite(texture);
    

    然后通过简单地调整您的 changeBG(int x) 方法。您可以适当地将精灵设置为您想要的区域。

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

    然后在你设置批处理之后在你的 draw() 方法中

    timeOfDay.draw(batch);
    

    我希望这会有所帮助。

    [更新]

    无需在渲染方法中绘制 DAY 和 NIGHT。 Time of Day 包含 DAY 和 NIGHT 纹理。当你打电话时

    timeOfDay.draw(batch); 
    

    它渲染 setRegion。

    你的渲染应该看起来像这样......

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

    【讨论】:

    • 感谢您的快速回复。我对java和libgdx不太熟悉。所以任何帮助都会受到我的赞赏。这就是我现在在我的AssetLoader中所拥有的...... Sprite timeOfDay = new Sprite(texture); public void changeBG(int x) { if (x
    • 在我的 GameRenderer....DAY.setRegion(291, 0, 287, 512); DAY.flip(假,真); batcher.draw(DAY, 0, midPointY -120, 136, 243); NIGHT.setRegion(0, 0, 287, 512); NIGHT.flip(假,真); batcher.draw(NIGHT, 0, midPointY -120, 136, 243); timeOfDay.draw(batcher);请需要一些帮助。
    • 您好 compulsivestudios 感谢您的帮助。我仍然有问题。当我在我的渲染绘制方法的地方我得到一个错误。在 timeOfDay 下。当我跑步时,我根本没有背景。游戏可以玩,但没有背景。 public void draw() { batcher.begin(); timeOfDay.draw(batcher);批处理器.end(); }
    • 作为调试,尝试在 init 某处将 timeOfDay Region 设置为 DAY。这样你就知道 timeOfDay 被默认设置为 DAY
    【解决方案2】:

    正如我在上一个答案中部分所说,您需要从头开始做的事情几乎如下:

    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:

    Sprite sprite = new Sprite(DAY);
    

    我猜你会将它设置为屏幕的大小,这取决于你是使用 Scene2d 还是正交变换,或者只是简单地使用屏幕坐标:

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

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

    然后,根据游戏的逻辑,您将要更改 TextureRegion。要存储您重试了多少次,您需要使用首选项:

    private static Preferences preferences; 
    
    @Override
    public void create()
    {
        preferences = Gdx.app.getPreferences(Resources.preferencesName);
        ...
    
    public static Preferences getPreferences()
    {
        return preferences;
    }
    

    之后,当游戏结束时,您执行以下操作:

    您在游戏结束时添加以下数字以更改尝试次数:

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

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

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

    【讨论】:

    • 感谢 Zhuinden,这就是我现在拥有的:私有静态首选项偏好; public void create() { prefs = Gdx.app.getPreferences("JoopklepsGame"); int currentTries = Gdx.app.getPreferences("JoopklepsGame").getInteger("numberOfTries");当前尝试++;当前尝试 %= 6; Gdx.app.getPreferences("JoopklepsGame").putInteger("numberOfTries", currentTries); Gdx.app.getPreferences("JoopklepsGame").flush(); changeBG(currentTries); } 公共静态首选项 getPreferences() { 返回首选项; }
    • sprite.setSize 放在哪里???我把所有东西都放在我的资产加载器中,我使用 Eclipse。问候 Jooklep。
    • 在创建 Sprite 之后,如果我没记错的话。但是如果你使用 AssetManager,那么你应该发布代码以便我们一起解决,因为我没有在我的项目中使用它。
    • 感谢Zhuinden的支持,我没有遇到任何错误,但它不能这样工作。也许我应该尝试使用随机方法。有两种纹理。一个白天和一个晚上的纹理,(texture1)(texture2) 也许你可以在这里帮忙,想想吧。现在开始工作。也许直到以后。 ;-)
    • Noooooo 拥有两个纹理不是一个好主意,拥有一个纹理是最好的。 “不工作”是什么意思?
    猜你喜欢
    • 2014-03-04
    • 2023-03-22
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多