在AndEngine无法加载精灵 [英] Can't load sprite in AndEngine

查看:102
本文介绍了在AndEngine无法加载精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习AndEngine,我遵循一些基本的教程。我试着在屏幕上加载一个精灵,而雪碧看起来损坏。

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.

这是我的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

}
}

这是它的外观:

我查了图像的大小为32X32,图像的路径,加载时,我也试着用不同的纹理选项。图像格式为PNG。如果你看到,背景颜色不适合与我设置的颜色。我觉得我的模拟器配置是否正确(与GPU仿真)与GLES2.0工作。
我认为这个问题可以在相机的高度和宽度摄像头的值。我设置的,因为我看到他们在本教程中,但不知道他们是否是正确的......我的分辨率是WVGA800。

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.

感谢您!

推荐答案

您应该延长 SimpleBaseGameActivity 来代替。然后,你不必担心那些回调或任何东西。

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

}

这篇关于在AndEngine无法加载精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆