无法在AndEngine中加载精灵

时间:2012-08-15 14:54:36

标签: android sprite andengine

我正在学习AndEngine,我正在学习一些基础教程。我试图在屏幕上加载一个精灵,但是Sprite看起来已经损坏了。

这是我的代码:

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

}
}

这就是它的样子:

Corrupted Sprite

我检查图像大小是32x32,图像的路径,我也尝试使用不同的纹理选项加载时。图像格式为PNG。如果看到,背景颜色与我设置的颜色不符。我认为我的模拟器已正确配置(使用GPU模拟)以与GLES2.0配合使用。 我认为问题可能是相机高度和相机宽度的值。我设置了这个,因为我在教程中看到了它们,但不知道它们是否正确...我的分辨率是WVGA800。

我不知道我做得不好......我需要解决这个问题才能继续制作游戏,所以任何帮助都会受到好评。

谢谢!

1 个答案:

答案 0 :(得分:4)

您应该扩展SimpleBaseGameActivity。然后你不必担心那些回调或任何东西。

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;
}

}