【问题标题】:Andengine loading graphics: why is my background texture small and upside downAndengine加载图形:为什么我的背景纹理小而倒置
【发布时间】: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 等图像编辑工具中缩小图像可能会更好。但无论哪种方式都应该很酷:)

标签: java android andengine


【解决方案1】:

我猜你图像的大小是 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

【讨论】:

  • 是的好地方,完全错过了他的 pTextureY 值。尽管如此,如果他需要从 10 开始的背景,他应该调整图形的大小以便这样做。顺便说一句,您的第一段中有两次“the”。
【解决方案2】:

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

正如erahal 指出的那样,如果您使用 1024x1024 图像(BitmapTextureAtlas 的大小),您将图像的高度属性偏移为 10,这将导致问题 - 因为您的新图像将被视为 1024x1034而不是 1024x1024 会抛出错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多