问题
Recently, I started working on android 2D game using Andengine on eclipse. When I try to load my background texture, I get this figure on my tablet (zync 930 plus) Here is my code: ResourceManager.java class
package com.example.parkmycar;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
public class ResourceManager {
private static final ResourceManager INSTANCE = new ResourceManager() ;
public MainGameActivity activity;
public Engine engine ;
public BoundCamera camera ;
public VertexBufferObjectManager vbom ;
//Textures
private BitmapTextureAtlas mainMenuTextureAtlas ;
public ITextureRegion playButton,mainMenuBackground ;
public void loadMenuResources(){
loadMenuGraphics() ;
//loadMenuSounds() ;
}
private void loadMenuGraphics(){
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mainMenuTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),1024,1024,TextureOptions.BILINEAR_PREMULTIPLYALPHA) ;
this.mainMenuBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,10) ;
//this.playButton = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,activity.getAssets(),"play.png") ;
this.mainMenuTextureAtlas.load();
/*try{
this.mainMenuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource,
BitmapTextureAtlas>(0,0,1) ) ;
//this.mainMenuTextureAtlas.load() ;
}catch(TextureAtlasBuilderException e){
Debug.e(e) ;
}*/
}
public static ResourceManager getInstance() {
return INSTANCE;
}
public static void prepareManager(Engine engine,MainGameActivity activity,BoundCamera camera,VertexBufferObjectManager vbom)
{
getInstance().engine = engine ;
getInstance().activity=activity ;
getInstance().camera = camera ;
getInstance().vbom = vbom ;
}
}
MainGameActivity.java class
package com.example.parkmycar;
import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;
public class MainGameActivity extends BaseGameActivity {
private BoundCamera camera ;//Bound to keep the camera focused on our player
private ResourceManager resourceManager ;
private float WIDTH=800 ;
private float HEIGHT=480 ;
@Override
public Engine onCreateEngine(EngineOptions engineOptions){
//Creating our customized engine
return new LimitedFPSEngine(engineOptions,60) ;
}
@Override
public EngineOptions onCreateEngineOptions() {
this.camera = new BoundCamera(0,0,WIDTH,HEIGHT) ;//posx,posy,width,height
//Methods to call all that we are going to need for our game (audio...)
EngineOptions engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new RatioResolutionPolicy(WIDTH,HEIGHT),this.camera) ;
//FillResolutionPolicy: structures the game to fill the resolution and devices
engineOptions.getAudioOptions().setNeedsMusic(true) ;
return engineOptions;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// Loads resources before the scene is shown (load textures, fonts,sounds...)==> Management
ResourceManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
resourceManager = ResourceManager.getInstance();
resourceManager.loadMenuResources();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// Where we are supposed to create our scene (called once the oncreateResources() is finished)
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
//Where we are supposed to populate our scene with buttons, background,text, entities...
}
}
I don't know what I am doing wrong.
回答1:
I am guessing that the size of you image is 1024x1024 (background.jpg ) and you are trying to fit in the the atlas texture with the height of 1024 but shifted by 10 ( parameters 0, 10 ).
So please try to replace this:
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,10) ;
With this:
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,0) ;
i.e: Do not shift by 10 the texture in the atlas if it has exactly the same size
回答2:
This usually happens because you are loading a texture (in this case background.jpg) that is bigger than your BitmapTextureAtlas area (1024x1024). Either scale your background image down or increase the size of BitmapTextureAtlas which is not suggested as 1024x1024 should be your max size. Anything bigger you may get a lot of performance issues.
Also as erahal pointed out, you are offsetting your image on the Height attribute of 10 which will cause issues if you are using a 1024x1024 image (size of your BitmapTextureAtlas
) -- because your new image will be treated as 1024x1034 instead of 1024x1024 and will throw an error.
来源:https://stackoverflow.com/questions/33851112/andengine-loading-graphics-why-is-my-background-texture-small-and-upside-down