2015-06-14 53 views
0

Im与Libgdx提供的AssetManager存在一些问题。 我得到一个空指针:通过AssetManager管理和加载资产(Libgdx)

Java.lang.IllegalArgumentException:纹理不能为null。

at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java) 
at com.test.test.screens.screens.MainScreen.show(MainScreen.java) 
at com.badlogic.gdx.Game.setScreen(Game.java) 
at com.test.test.screens.screens.SplashScreen.render(SplashScreen.java) 

从来就检查和文件it's负荷存在,并且正确的,所以it's在我的代码的东西。我实际上不知道该怎么做。我被告知要确保我没有创建一个新的资产实例,而是创建一个现有的实例。不知道是否从来就做到了,虽然正确.. 这是类它的自我:

public class Assets { 

    public final AssetManager manager = new AssetManager(); 
    private ObjectMap<String, Texture> textures; 
    private ObjectMap<String, Sound> sounds; 
    public final String background = "test.jpg"; 

public Assets() { 
    textures = new ObjectMap<String, Texture>(); 
    sounds = new ObjectMap<String, Sound>(); 
    manager.load(background, Texture.class); 

} 

public boolean update() { 
    boolean done = manager.update(); 
    if (done) { 
     finishLoading(); 

    } 
    return done; 
} 

private void finishLoading() { 
    textures.put(background, manager.get(background, Texture.class)); 

} 

public Texture getTexture(String name) { 
    return textures.get(name); 
} 
    public void dispose() { 

     manager.clear(); 

    } 
} 

而此刻我宣布它像这样在我MainClass:

public class MainClass extends Game { 
public SpriteBatch batch; 
public purchaseInterface pi; 
//Calls the Assets to be implemented in other classes 
public Assets assets; 
public MainClass(purchaseInterface purchase, GalleryOpener opener){ 
    this.gallery= opener; 
    this.pi = purchase; 
} 
@Override 
public void create() { 
    batch = new SpriteBatch(); 
    assets = new Assets(); 
    setScreen(new SplashScreen(this)); 
    } 

@Override 
public void resize(int width, int height) { 
    super.resize(width, height); 

} 

@Override 
public void render() { 
    super.render(); 


} 

@Override 
public void dispose() { 
    super.dispose(); 
    batch.dispose(); 
    assets.dispose(); 
} 

public Assets getAssets() { 
    return assets; 
} 


@Override 
public void pause() { 
    super.pause(); 
} 


@Override 
public void resume() { 
    // TODO Auto-generated method stub 
    super.resume(); 
} 

} 而用于装载资产到屏幕类例如:

public Assets assets; 
public MainScreen(MainClass gam) { 
    game = gam; 
    assets = game.getAssets(); 
    loadStore(); 
    camera = new OrthographicCamera(screenWidth,screenHeight); 
    view = new StretchViewport(screenWidth, screenHeight, camera); 
    view.apply(); 
    camera.translate(camera.viewportWidth/2, camera.viewportHeight/2); 

} 

public void loadStore() { 
    background = assets.getTexture(assets.background); 
} 



@Override 
public void render(float delta) { 

    camera.update(); 

    game.batch.setProjectionMatrix(camera.combined); 

    game.batch.begin(); 

    game.batch.draw(background, 0, 0, 1000, 2000); 

    game.batch.end(); 

} 

@Override 
public void resize(int width, int height) { 
    view.update(width, height, true); 
} 

@Override 
public void show() { 
} 




@Override 
public void hide() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void pause() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void resume() { 
} 

@Override 
public void dispose() { 
    background.dispose(); 
} 

}

回答

0

这不会加载背景纹理:

manager.load(background, Texture.class); 

您需要致电

manager.finishLoading(); 

负载()来之后。 AssetManager.load()只是存储资产的路径。 AssetManager.update()从另一个线程的存储路径加载下一个项目,AssetManager.finishLoading()加载所有项目并等待加载线程完成。当您想要在加载其他资源时绘制图像时,需要首先加载该图像(本例中为“背景”)。

另一件事,我认为你存储的东西两次没有(纹理,声音objectmaps)。最好的做法是使用资产管理器来获取具有“获取”功能的纹理或任何资产。

我这样做:

public class LoadingScreen extends Screen { 

    ... 

    @Override 
    public void show() { 
     app.assets.load("data/textures/loading.pack", TextureAtlas.class); 
     app.assets.finishLoading(); // this is waits for the loading finish 

     app.assets.load("data/textures/menu.pack", TextureAtlas.class); 
     app.assets.load("data/textures/sprites.pack", TextureAtlas.class); 
     ... 

    } 

    @Override 
    public void render(float delta) { 
     if (app.assets.update()) { // this is loads the next item in an other thread 
      app.loadingFinished(); // this is where you will create the other screens 
     }    
     ... 
    } 

    ... 

“应用程序” 是一个游戏实例, “app.assets” 是AssetManager实例。当我想拥有一项资产时,我这样做(但是这只能在加载完成后运行!):

TextureAtlas atlas = app.assets.get("data/textures/sprites.pack", TextureAtlas.class);