[英]Andengine loading graphics: why is my background texture small and upside down
最近,我開始在eclipse上使用Andengine從事android 2D游戲。 當我嘗試加載背景紋理時,我在平板電腦上看到了這個數字(zync 930 plus) 這是我的代碼: ResourceManager.java類
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類
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...
}
}
我不知道我在做什么錯。
我猜想您的圖像大小為1024x1024(background.jpg),並且您正試圖適應高度為1024的Atlas紋理,但偏移了10(參數0、10)。
因此,請嘗試替換為:
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,10) ;
有了這個:
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,0) ;
即:如果地圖集的大小完全相同,則不要將其偏移10
這通常是因為加載的紋理(在本例中為background.jpg)大於BitmapTextureAtlas區域(1024x1024)。 縮小背景圖像或增加BitmapTextureAtlas的大小(不建議這樣做,因為1024x1024應該是最大大小)。 更大的東西可能會遇到很多性能問題。
另外,正如ehahal指出的那樣,您將圖像偏移在Height屬性的10上,如果您使用的是BitmapTextureAtlas
圖像( BitmapTextureAtlas
大小),則會導致問題-因為新圖像將被視為1024x1034而不是1024x1024,並且拋出一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.