Can't load sprite in AndEngine

给你一囗甜甜゛ 提交于 2019-12-23 04:18:09

问题


I'm learning AndEngine and I'm following some basic tutorials. I tried to load an sprite in the screen, but the Sprite looks corrupted.

This is my code:

package com.example.andengine_demo;

import org.andengine.engine.camera.Camera;
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.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
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.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

public class MainActivity extends BaseGameActivity {

    // ===========================================================
    // Constants
    // ===========================================================
    static final int CAMERA_WIDTH = 720; 
    static final int CAMERA_HEIGHT = 480;

    private static final String TAG = "AndEngineTest";
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private TextureRegion mPlayerTextureRegion;
    // ===========================================================
    // Fields
    // ===========================================================

    //private ZoomCamera mCamera;



@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
    // TODO Auto-generated method stub
    mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
    mBitmapTextureAtlas.load();

}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
{
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // TODO Auto-generated method stub
    Scene scene = new Scene();
    scene.setBackground(new Background(0f, 0f, 1f));

    final Sprite oPlayer = new Sprite(32,32, mPlayerTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(oPlayer);
}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    // TODO Auto-generated method stub

}
}

And this is how it looks:

I checked the image size is 32x32, the path of the image and I've also tried with different Texture Options when loading. The image format is PNG. If you see, the background color doesn't fit with the color I set. I think my emulator is correctly configured (with GPU emulation) to work with GLES2.0. I thought the problem can be the values on camera height and camera width. I set that because I saw them on the tutorial, but don't know if they are correct... My resolution is WVGA800.

I don't know what I'm doing bad... I need to solve this problem to continue creating games, so any help will be well received.

Thank you!


回答1:


You should be extending SimpleBaseGameActivity instead. Then you don't have to worry about those callbacks or anything.

public class MainActivity extends SimpleBaseGameActivity {

// ===========================================================
// Constants
// ===========================================================
static final int CAMERA_WIDTH = 720; 
static final int CAMERA_HEIGHT = 480;

private static final String TAG = "AndEngineTest";
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
// ===========================================================
// Fields
// ===========================================================

//private ZoomCamera mCamera;



@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}

@Override
public void onCreateResources() {
    mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
    mBitmapTextureAtlas.load();
}

@Override
public Scene onCreateScene()
{
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // TODO Auto-generated method stub
    Scene scene = new Scene();
    scene.setBackground(new Background(0f, 0f, 1f));

    final Sprite oPlayer = new Sprite(32,32, mPlayerTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(oPlayer);
    return scene;
}

}


来源:https://stackoverflow.com/questions/11971674/cant-load-sprite-in-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!