java和引擎加载图形:为什么我的背景纹理很小,而且是颠倒的
最近,我开始在eclipse上使用Andengine开发安卓 2D游戏。 当我尝试加载我的背景纹理时,我在平板电脑(zync 930 plus)上看到了这个数字 这是我的代码: 资源管理器。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 ;
}
}
主要游戏活动。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...
}
}
我不知道我做错了什么
# 1 楼答案
这通常是因为加载的纹理(在本例中为background.jpg)大于BitmapTextureAtlas区域(1024x1024)。缩小背景图像或增大BitmapTextureAtlas的大小,但不建议将其作为1024x1024的最大大小。任何更大的问题都可能会带来很多性能问题
同样正如erahal所指出的,如果使用1024x1024图像(你的
BitmapTextureAtlas
的大小),你的图像将在高度属性10上偏移,这将导致问题,因为你的新图像将被视为1024x1034,而不是1024x1024,并将抛出一个错误# 2 楼答案
我猜你的图像大小是1024x1024(background.jpg),你正在尝试适应atlas纹理,高度是1024,但移动了10(参数0,10)
所以请尝试替换这个:
有了这个:
即:如果图集中的纹理大小完全相同,则不要移动10