这是我第一次使用 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);
这是日志猫:
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