1

我是android游戏开发的新手,经过搜索,我终于发现我必须使用andengine,我已经开始了,但我面临一些问题,我有2个类,一个是主要活动,它的代码如下

package com.game.day1v1;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import com.game.day1v1.SceneManager.SceneType;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends BaseGameActivity {

public SceneManager sceneManager ;
public Camera mCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {

    // TODO Auto-generated method stub
    sceneManager = new SceneManager(this, mEngine, mCamera);
    sceneManager.loadSplashSceneResources();

    pOnCreateResourcesCallback.onCreateResourcesFinished();


}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {
    // TODO Auto-generated method stub

         pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());

}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception        { 
    // TODO Auto-generated method stub
    mEngine.registerUpdateHandler(new TimerHandler(1f, new ITimerCallback(){
    public void onTimePassed(final TimerHandler pTimerHandler)
    {
        mEngine.unregisterUpdateHandler(pTimerHandler);
        sceneManager.loadGameSceneResources();
        sceneManager.createGameScenes();
        sceneManager.setCurrentScene(SceneType.TITLE);
    }
}));
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

}

另一个文件是场景管理器,其代码如下

package com.game.day1v1;

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

public class SceneManager {

private SceneType currentScene;
private BaseGameActivity activity;
private Engine engine;
private Camera camera;
public Scene splashScene;
public BitmapTextureAtlas splashTextureAtlas ;
public TextureRegion splashTextureRegion;
public Sprite splash ;
public Scene titleScene ;
public Scene mainGameScene;

public enum SceneType
{
    SPLASH,
    TITLE,
    MAINGAME
}

public SceneManager(BaseGameActivity activity, Engine engine, Camera camera) {
    this.activity = activity;
    this.engine = engine;
    this.camera = camera;
}

//Method loads all of the splash scene resources
public void loadSplashSceneResources() {

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("assets/gfx/");
    splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(),  256, 256, TextureOptions.DEFAULT);
    splashTextureRegion =  BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
    splashTextureAtlas.load();
}

//Method loads all of the resources for the game scenes
public void loadGameSceneResources() {

}

//Method creates the Splash Scene
public Scene createSplashScene() {

    //Create the Splash Scene and set background colour to red and add the splash logo.
     splashScene = new Scene();
    splashScene.setBackground(new Background(1, 0, 0));
    splash = new Sprite(0, 0, splashTextureRegion,  activity.getVertexBufferObjectManager())
    {
        @Override
        protected void preDraw(GLState pGLState, Camera pCamera)
        {
            super.preDraw(pGLState, pCamera);
            pGLState.enableDither();
        }
    };
    splash.setScale(1.5f);
    splash.setPosition((camera.getWidth() - splash.getWidth()) * 0.5f, (camera.getHeight() - splash.getHeight()) * 0.5f);
    splashScene.attachChild(splash);

    return splashScene;
}

//Method creates all of the Game Scenes
public void createGameScenes() {

    //Create the Title Scene and set background colour to green
    titleScene = new Scene();
    titleScene.setBackground(new Background(0, 1, 0));

    //Create the Main Game Scene and set background colour to blue
    mainGameScene = new Scene();
    mainGameScene.setBackground(new Background(0, 0, 1));

}

//Method allows you to get the currently active scene
public SceneType getCurrentScene() {
    return currentScene;
}

//Method allows you to set the currently active scene
public void setCurrentScene(SceneType scene) {

    currentScene = scene;
    switch (scene)
    {
    case SPLASH:
        break;
    case TITLE:
        engine.setScene(titleScene);
        break;
    case MAINGAME:
        engine.setScene(mainGameScene);
        break;
    }
}


}

但我在我的 logcat 中面临以下异常

09-08 12:53:21.598: E/AndroidRuntime(278): FATAL EXCEPTION: main
09-08 12:53:21.598: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.game.day1v1/com.game.day1v1.MainActivity}: java.lang.NullPointerException
09-08 12:53:21.598: E/AndroidRuntime(278):  at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-08 12:53:21.598: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
09-08 12:53:21.598: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
09-08 12:53:21.598: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-08 12:53:21.598: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-08 12:53:21.598: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
09-08 12:53:21.598: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
09-08 12:53:21.598: E/AndroidRuntime(278):  at org.andengine.engine.Engine.<init>(Engine.java:142)
09-08 12:53:21.598: E/AndroidRuntime(278):  at org.andengine.ui.activity.BaseGameActivity.onCreateEngine(BaseGameActivity.java:90)
09-08 12:53:21.598: E/AndroidRuntime(278):  at org.andengine.ui.activity.BaseGameActivity.onCreate(BaseGameActivity.java:80)
09-08 12:53:21.598: E/AndroidRuntime(278):  at com.game.day1v1.MainActivity.onCreate(MainActivity.java:22)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 12:53:21.598: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

09-08 12:53:21.598: E/AndroidRuntime(278): ... 11 更多

谁能告诉我它的原因?

4

2 回答 2

3

BaseGameActivity你不覆盖OnCreate. 要开始使用 andengine,请遵循本教程: Andengine 游戏教程

于 2013-09-08T07:39:58.660 回答
0

在您的创建场景中添加这个精灵()

splash1 = new Sprite(0, 0, resourcesManager.menu_background_region,
                vbom) {
            @Override
            protected void preDraw(GLState pGLState, Camera pCamera) {
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
        };

        // splash1.setScale(.47f);
        splash1.setScale(.9f);

        splash1.setPosition(400, 240);
        attachChild(splash1);
于 2013-11-30T06:12:36.310 回答