AndEngine创建一个矩形崩溃的应用程序

时间:2013-08-04 04:34:20

标签: android button andengine rectangles touch-event

这是我第一次使用AndEngine,当我尝试创建一个Rectangle时,它会在该行上崩溃。我正在尝试使用矩形按钮向左或向右移动播放器。我在这里找到了一个关于按钮的教程:http://www.matim-dev.com/creating-leftright-game-controller-buttons.html。我甚至尝试为场景使用触摸事件,而是在那里崩溃。我找了很长时间的答案,但仍然找不到答案所以现在我完全卡住了。任何帮助将不胜感激。

HUD controller = new HUD();
    Rectangle left = new Rectangle(0, 0, (screenWidth / 2), (screenHeight), getVertexBufferObjectManager()) // It crashes right on this line.
    {
        @Override
        public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
        {
            // Move left
            if (touchEvent.isActionDown())
            {
                isLeftTouched = true;
            }
            if (touchEvent.isActionUp())
            {
                isLeftTouched = false;
                player1.stop();
            }
            return true;
        }

        @Override
        public void onManagedUpdate(float secondsElapsed)
        {
            if (isLeftTouched)
            {
                // Execute your actions.
                player1.moveLeft();

            }
            super.onManagedUpdate(secondsElapsed);
        }
    };

    Rectangle right = new Rectangle(screenWidth / 2, 0, screenWidth / 2, screenHeight, getVertexBufferObjectManager())
    {
        @Override
        public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
        {
            // Move right
            if (touchEvent.isActionDown())
            {
                isRightTouched = true;
            }
            if (touchEvent.isActionUp())
            {
                isRightTouched = false;
                player1.stop();
            }
            return true;
        }

        @Override
        public void onManagedUpdate(float secondsElapsed)
        {
            if (isRightTouched)
            {
                // Execute your actions.
                player1.moveRight();
            }
            super.onManagedUpdate(secondsElapsed);
        }
    };

    controller.registerTouchArea(left);
    controller.registerTouchArea(right);
    controller.attachChild(left);
    controller.attachChild(right);

    camera.setHUD(controller);

这是logcat:

E/AndroidRuntime( 2440):    java.lang.RuntimeException: Unable  to start activity   ComponentInfo{com.teej1410.glider/  com.teej1410.glider.MainActivity}:  java.lang.NullPointerException
E/AndroidRuntime( 2440):    at  com.teej1410.glider.MainActivity.createControllers(MainActivity.java:169)
E/AndroidRuntime( 2440):    at  com.teej1410.glider.MainActivity.onCreateOther(MainActivity.java:132)
E/AndroidRuntime( 2440):    at  com.teej1410.glider.MainActivity.onCreateEngineOptions(MainActivity.java:   51)
W/ActivityManager(  434):   Force   finishing

1 个答案:

答案 0 :(得分:1)

我修好了。我有一个单独的方法创建矩形,我打电话太早了。