문제

I am trying to create a simple scene and show it but it won't work and I can't find what I am doing wrong.

Here is my main activity

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

}

All I can see on my phone is a black screen (the rectangles are not there), the "onManagedUpdate" does not show and touching the screen won't call onSceneTouchEvent.

Why is that so?

도움이 되었습니까?

해결책

Return true for onSceneTouchEvent calling. Finishe callback methods when they transfer one method to other in Engine Life Cycle methods. Add below 3 line to corresponding methods.

pOnCreateResourcesCallback.onCreateResourcesFinished(); after onCreateResources() finished

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

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

Like:

@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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top