0

我正在尝试创建一个简单的场景并展示它,但它不起作用,我找不到我做错了什么。

这是我的主要活动

public class MainActivity extends BaseGameActivity implements IOnSceneTouchListener{
final static String TAG = "BounceTest";
Scene scene;
float downX;
float downY;

@Override
public EngineOptions onCreateEngineOptions() {
    Log.d(TAG, "onCreateEngineOptions");
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), new Camera(0, 0, 800, 480));
}

@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
    Log.d(TAG, "onCreateResources");
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
    Log.d(TAG, "onCreateScene");
    scene = new Scene(){
        @Override
        protected void onManagedUpdate(float pSecondsElapsed) {
            Log.d(TAG, "onManagedUpdate");
            super.onManagedUpdate(pSecondsElapsed);
        }
    };
    this.getEngine().setScene(scene);
    scene.setOnSceneTouchListener(this);
}

@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    Log.d(TAG, "onPopulateScene");
    Rectangle floor = new Rectangle(-200, 0, 1000, 20, this.getVertexBufferObjectManager());
    Rectangle square = new Rectangle(0, 20, 20, 20, this.getVertexBufferObjectManager());
    scene.attachChild(floor);
    scene.attachChild(square);
}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    Log.d(TAG, "onSceneTouchEvent");
    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
        downX = pSceneTouchEvent.getX();
        downY = pSceneTouchEvent.getY();
    }else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
        float upX = pSceneTouchEvent.getX();
        float upY = pSceneTouchEvent.getY();
        float xDiff = downX - upX;
        float yDiff = downY - upY;
        float dragLength = (float)Math.sqrt(xDiff*xDiff+yDiff*yDiff);
        Log.d(TAG, "dragLength:"+dragLength);
    }

    return false;
}

}

我在手机上只能看到黑屏(矩形不存在),“onManagedUpdate”不显示,触摸屏幕不会调用 onSceneTouchEvent。

为什么呢?

4

1 回答 1

3

对 onSceneTouchEvent 调用返回true。在引擎生命周期方法中将一种方法转移到另一种方法时,完成回调方法。将以下 3 行添加到相应的方法中。

pOnCreateResourcesCallback.onCreateResourcesFinished(); after onCreateResources() finished

pOnCreateSceneCallback.onCreateSceneFinished(mScene); after onCreateScene() finished

pOnPopulateSceneCallback.onPopulateSceneFinished(); after onPopulateScene() finished.

像:

@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
    Log.d(TAG, "onCreateResources");
    // your code will be here 
    // call this callback;
    pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
    Log.d(TAG, "onCreateScene");

    // your code will be here 
    // call this callback;
    pOnCreateSceneCallback.onCreateSceneFinished(mScene);
}

@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    Log.d(TAG, "onPopulateScene");

    // your code will be  here

    // call this callback;
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}
于 2013-07-14T06:48:48.123 回答