“Andengine

时间:2015-05-29 13:28:05

标签: andengine

我有2个文件:

MainActivity.java

public class MainActivity extends BaseGameActivity implements
    IOnMenuItemClickListener {

/*
 * Constants for frame
 */
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

protected static final int MENU_ABOUT = 0;
protected static final int MENU_QUIT = MENU_ABOUT + 1;
protected static final int MENU_PLAY = 100;
protected static final int MENU_SCORES = MENU_PLAY + 1;
protected static final int MENU_OPTIONS = MENU_SCORES + 1;
protected static final int MENU_HELP = MENU_OPTIONS + 1;

/*
 * Fields
 */
private Camera mCamera;
private Scene mMainScene;

// Save Image for Menu
private BitmapTextureAtlas mMenuBackTexture;
private TextureRegion mMenuBackTextureRegion;

private BitmapTextureAtlas mPopUpTexture;
// private BitmapTextureAtlas mFontTexture;

protected Font mFont;

protected TextureRegion mPopUpAboutTextureRegion;
protected TextureRegion mPopUpQuitTextureRegion;
protected TextureRegion mMenuPlayTextureRegion;
protected TextureRegion mMenuScoresTextureRegion;
protected TextureRegion mMenuOptionsTextureRegion;
protected TextureRegion mMenuHelpTextureRegion;

private boolean popupDisplayed;
protected MenuScene mStaticMenuScene, mPopUpMenuScene;

/*
 * Constructors
 */

/*
 * Getter and Setter
 */
public Camera getmCamera() {
    return mCamera;
}

public void setmCamera(Camera mCamera) {
    this.mCamera = mCamera;
}

/*
 * Methods for SuperClass/Interface
 */

@Override
public Engine onLoadEngine() {
    // TODO Auto-generated method stub
    // this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    this.setmCamera(new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT));
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            this.getmCamera()));
}

@Override
public void onLoadResources() {
    // TODO Auto-generated method stub
    // this.mTexture = new BitmapTextureAtlas(512, 512,
    // TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    // this.mSplashTextureRegion = BitmapTextureAtlasTextureRegionFactory
    // .createFromAsset(this.mTexture, this, "gfx/Splashscreen.png",
    // 0, 0);
    // this.mEngine.getTextureManager().loadTexture(this.mTexture);

    // Process and load Font Menu
    final ITexture mFontTexture = new BitmapTextureAtlas(256, 256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    FontFactory.setAssetBasePath("font/");

    this.mFont = FontFactory.createFromAsset(mFontTexture, this,
            "Vera.ttf", 32, true, Color.RED);
    this.mFont.reload();
    this.mEngine.getTextureManager().loadTexture(mFontTexture);
    this.mEngine.getFontManager().loadFont(this.mFont);

    // Process image background
    this.mMenuBackTexture = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mMenuBackTexture, this,
                    "gfx/MainMenu/MainMenuBk.png", 0, 0);
    this.mEngine.getTextureManager().loadTexture(this.mMenuBackTexture);

    // Process image PopUp
    this.mPopUpTexture = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mPopUpAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mPopUpTexture, this,
                    "gfx/MainMenu/About_button.png", 0, 0);
    this.mPopUpAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mPopUpTexture, this,
                    "gfx/MainMenu/Quit_button.png", 0, 50);

    this.mEngine.getTextureManager().loadTexture(this.mPopUpTexture);
    popupDisplayed = false;
}

@Override
public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    // final Scene scene = new Scene();

    this.createStaticMenuScene();
    this.createPopUpMenuScene();

    /* Center the splash on the camera. */
    final int centerX = (CAMERA_WIDTH - this.mMenuBackTextureRegion
            .getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mMenuBackTextureRegion
            .getHeight()) / 2;

    this.mMainScene = new Scene();

    /* Create the sprite and add it to the scene. */
    final Sprite menuBack = new Sprite(centerX, centerY,
            this.mMenuBackTextureRegion);
    mMainScene.attachChild(menuBack);
    return mMainScene;
}

@Override
public void onLoadComplete() {
    // TODO Auto-generated method stub

}

@Override
public boolean onKeyDown(int pKeyCode, KeyEvent pEvent) {
    if (pKeyCode == KeyEvent.KEYCODE_MENU
            && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (popupDisplayed) {
            // Remove the menu and reset it.
            this.mPopUpMenuScene.back();
            mMainScene.setChildScene(mStaticMenuScene);
            popupDisplayed = false;
        } else {
            this.mMainScene.setChildScene(mPopUpMenuScene, false, true,
                    true);
            popupDisplayed = true;
        }
        return true;
    } else {
        return super.onKeyDown(pKeyCode, pEvent);
    }
}

@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
        float pMenuItemLocalX, float pMenuItemLocalY) {
    switch (pMenuItem.getID()) {
    case MENU_ABOUT:
        Toast.makeText(MainActivity.this, "About selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_QUIT:
        this.finish();
        return true;
    case MENU_PLAY:
        Toast.makeText(MainActivity.this, "Play selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_SCORES:
        Toast.makeText(MainActivity.this, "Scores selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_OPTIONS:
        Toast.makeText(MainActivity.this, "Options selected",
                Toast.LENGTH_SHORT).show();
        return true;
    case MENU_HELP:
        Toast.makeText(MainActivity.this, "Help selected",
                Toast.LENGTH_SHORT).show();
        return true;
    default:
        return false;
    }
}

/*
 * Methods for this class
 */
protected void createStaticMenuScene() {
    // put camera to menu
    this.mStaticMenuScene = new MenuScene(this.mCamera);

    // Add Play game
    final IMenuItem playMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_PLAY, mFont, "Play Game"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    playMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(playMenuItem);

    // Add Scores
    final IMenuItem scoresMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_SCORES, mFont, "Scores"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    scoresMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(scoresMenuItem);

    // Add Options
    final IMenuItem optionsMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_OPTIONS, mFont, "Options"), 0.5f, 0.5f,
            0.5f, 0.0f, 0.0f, 0.0f);
    optionsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(optionsMenuItem);

    // Add Help
    final IMenuItem helpMenuItem = new ColorMenuItemDecorator(
            new TextMenuItem(MENU_HELP, mFont, "Help"), 0.5f, 0.5f, 0.5f,
            0.0f, 0.0f, 0.0f);
    helpMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mStaticMenuScene.addMenuItem(helpMenuItem);

    // build camera
    this.mStaticMenuScene.buildAnimations();
    this.mStaticMenuScene.setBackgroundEnabled(false);
    this.mStaticMenuScene.setOnMenuItemClickListener(this);
}

protected void createPopUpMenuScene() {
    this.mPopUpMenuScene = new MenuScene(this.mCamera);

    //add about popup
    final SpriteMenuItem aboutMenuItem = new SpriteMenuItem(MENU_ABOUT,
            this.mPopUpAboutTextureRegion);
    aboutMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mPopUpMenuScene.addMenuItem(aboutMenuItem);

    //add quit popup
    SpriteMenuItem quitMenuItem = new SpriteMenuItem(MENU_QUIT,
            this.mPopUpQuitTextureRegion);
    quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
            GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mPopUpMenuScene.addMenuItem(quitMenuItem);

    //build camera 
    this.mPopUpMenuScene.setMenuAnimator(new SlideMenuAnimator());
    this.mPopUpMenuScene.buildAnimations();
    this.mPopUpMenuScene.setBackgroundEnabled(false);
    this.mPopUpMenuScene.setOnMenuItemClickListener(this);

}
}

StartActivity.java

public class StartActivity extends BaseGameActivity {

/*
 * Constants
 */

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

/*
 * Fields
 */

private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mTextureRegion;
private Handler mHandler;

/*
 * Constructors
 */

/*
 * Getter and Setter
 */

/*
 * Method for/from SuperClass/Interfaces
 */

@Override
public Engine onLoadEngine() {
    mHandler = new Handler();
    // put camera
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            this.mCamera));
}

@Override
public void onLoadResources() {
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this, "gfx/Splashscreen.png", 0, 0);
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

@Override
public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    /* Center the splash on the camera. */
    final int centerX = (CAMERA_WIDTH - this.mTextureRegion
            .getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mTextureRegion
            .getHeight()) / 2;
    final Sprite slash = new Sprite(centerX, centerY,
            this.mTextureRegion);
    scene.attachChild(slash);
    return scene;       
}

@Override
public void onLoadComplete() {      
    mHandler.postDelayed(mLauchTask, 3000);
}




/*
 * Methods
 */

/*
 * Inner and Anonymous Classes
 */
private Runnable mLauchTask = new Runnable() {
    public void run() {
        Intent myIntent = new Intent(StartActivity.this, MainActivity.class);
        StartActivity.this.startActivity(myIntent);
    }
};
}

我在logcat中看到

  

05-29 18:44:50.338:W / dalvikvm(1049):threadid = 1:线程退出,未捕获异常(group = 0x4001d800)   05-29 18:44:50.358:E / AndroidRuntime(1049):致命异常:主要   05-29 18:44:50.358:E / AndroidRuntime(1049):java.lang.NullPointerException   05-29 18:44:50.358:E / AndroidRuntime(1049):at org.anddev.andengine.entity.sprite.Sprite。(Sprite.java:27)   05-29 18:44:50.358:E / AndroidRuntime(1049):at org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem。(SpriteMenuItem.java:29)   05-29 18:44:50.358:E / AndroidRuntime(1049):at com.khoado.v3.MainActivity.createPopUpMenuScene(MainActivity.java:285)   05-29 18:44:50.358:E / AndroidRuntime(1049):at com.khoado.v3.MainActivity.onLoadScene(MainActivity.java:154)   05-29 18:44:50.358:E / AndroidRuntime(1049):at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:169)   05-29 18:44:50.358:E / AndroidRuntime(1049):at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)   05-29 18:44:50.358:E / AndroidRuntime(1049):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onWindowFocusChanged(PhoneWindow.java:1981)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.view.View.dispatchWindowFocusChanged(View.java:3788)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:658)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.view.ViewRoot.handleMessage(ViewRoot.java:1921)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.os.Handler.dispatchMessage(Handler.java:99)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.os.Looper.loop(Looper.java:123)   05-29 18:44:50.358:E / AndroidRuntime(1049):在android.app.ActivityThread.main(ActivityThread.java:4627)   05-29 18:44:50.358:E / AndroidRuntime(1049):at java.lang.reflect.Method.invokeNative(Native Method)   05-29 18:44:50.358:E / AndroidRuntime(1049):at java.lang.reflect.Method.invoke(Method.java:521)   05-29 18:44:50.358:E / AndroidRuntime(1049):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   05-29 18:44:50.358:E / AndroidRuntime(1049):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)   05-29 18:44:50.358:E / AndroidRuntime(1049):at dalvik.system.NativeStart.main(Native Method)

为什么会出错?

1 个答案:

答案 0 :(得分:0)

您的createPopUpMenuScene方法中已获得Null Pointer Exception。生成此异常的行号是285.无论如何,因为我无法确定您发布的第285行,所以我无法给出明确而直截了当的答案。

在我看来,通过调试应用程序来逐步调试代码行,可以轻松解决这些问题。