【发布时间】:2015-11-22 02:50:53
【问题描述】:
最近,我开始在 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...
}
}
我不知道我做错了什么。
【问题讨论】:
-
这通常是因为您加载的纹理(在本例中为 background.jpg)大于您的 BitmapTextureAtlas 区域。要么缩小背景图像,要么增加 BitmapTextureAtlas 的大小,这是非常不建议的。
-
谢谢!!我缩放了我的图像,scene.setScale(1.5f),它起作用了!!!
-
太棒了,没问题。虽然我不确定 setScale 是否理想(如果你想最小化处理时间),因为在 GIMP 等图像编辑工具中缩小图像可能会更好。但无论哪种方式都应该很酷:)