在andengine中加载背景

时间:2013-09-08 07:02:20

标签: android andengine

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

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更多

任何人都可以告诉我它的原因吗?

2 个答案:

答案 0 :(得分:3)

BaseGameActivity中,您不会覆盖OnCreate。 要从andengine开始,请遵循以下教程: Andengine game turorial

答案 1 :(得分: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);