Andengine加载图形:为什么我的背景纹理很小且颠倒

时间:2015-11-22 02:50:54

标签: java android andengine

最近,我开始在eclipse上使用Andengine开发Android 2D游戏。 当我尝试加载我的背景纹理时,我在平板电脑上得到这个数字(zync 930 plus) enter image description here 这是我的代码: 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...

    } 
}

我不知道自己做错了什么。

2 个答案:

答案 0 :(得分:0)

我猜你的图像大小是1024x1024(background.jpg)并且你试图适应高度为1024但移动了10的atlas纹理(参数0,10)。

所以请尝试替换它:

BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,10) ;

有了这个:

BitmapTextureAtlasTextureRegionFactory.createFromAsset(mainMenuTextureAtlas,this.activity,"background.jpg",0,0) ;

即:如果它具有完全相同的大小

,请不要将图层中的纹理移动10

答案 1 :(得分:0)

这通常是因为您正在加载比BitmapTextureAtlas区域(1024x1024)更大的纹理(在本例中为background.jpg)。要么缩小你的背景图像,要么增加BitmapTextureAtlas的大小,这是不建议的,因为1024x1024应该是你的最大尺寸。任何更大的东西你都可能会遇到很多性能问题。

同样如同有一个指示,你会在10的高度属性上偏移你的图像,如果你使用1024x1024图像(BitmapTextureAtlas的大小)会导致问题 - 因为你的新图像会被处理为1024x1034而不是1024x1024并将引发错误。