0

我正在使用 JME3 和 Nifty GUI 开发游戏。我有一个外部类,它有一个Nifty成员变量。无论访问修饰符如何,内部类都应该能够访问该变量。在构造函数中,我为其分配了一个新Nifty对象。但是,当我在内部类中访问该变量时,我遇到了问题。我做了一点调试,发现这是因为内部类认为Nifty成员变量为空,我不知道为什么。它在外部类中不为空。由于这是一个 JME3 游戏,我试图让内部类实现AppState接口,但它仍然将Nifty成员变量显示为 null。这是代码:

public class MenuScreen extends SimpleApplication {



/** Used to configure Nifty GUI. */
private Nifty mNifty;

private NiftyJmeDisplay mNiftyDisplay;


private Element popup;

//*******************
// Overridden medhods
//*******************

/** This method is used to initialize everything needed to display the game screen. */
@Override
public void simpleInitApp() {
    guiNode.detachAllChildren();

    initNifty();

    flyCam.setDragToRotate(true);
}

/**
 * The game's main update loop.
 * 
 * @param tpf Time Per Fram, the time it takes each loop to run.
 */
@Override
public void simpleUpdate(float tpf) {
    // not used
}

@Override
public void simpleRender(RenderManager rm) {
    // not used
}

public static void main(String[] args) {
    MenuScreen app = new MenuScreen();
    app.start();
}

/**
* Helper method to initialize and configure Nifty GUI.
*/
private void initNifty() {        
    mNiftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
    mNifty = mNiftyDisplay.getNifty();

    guiViewPort.addProcessor(mNiftyDisplay);

    // If this is being run on a desktop then load the desktop main menu.
    if (Strings.OS_NAME.contains("windows") || Strings.OS_NAME.contains("mac") || Strings.OS_NAME.contains("linux")) {
        mNifty.fromXml("Interface/XML/DesktopMenuScreenGui.xml", "start", new MenuScreen().new MenuScreenGui());
    }
    // If its an Android device load the mobile main menu.
    else if (Strings.OS_NAME.contains("android")) {
        mNifty.fromXml("Interface/XML/MobileMenuScreenGui.xml", "mobile", new MenuScreen().new MenuScreenGui());
    }
}

//**************
// Inner Classes
//**************

/**
 * © Jason Crosby 2012 <p>
 * 
 * This class handles all the GUI interactions like button clicks.
 * 
 * @author Jason Crosby
 */
public class MenuScreenGui implements ScreenController, EventTopicSubscriber<MenuItemActivatedEvent>,
                                      AppState {

    @Override
    public void initialize(AppStateManager stateManager, Application app) {

    }
@Override
    public void cleanup() {

    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public boolean isInitialized() {
        return false;
    }

    @Override
    public void postRender() {

    }

    @Override
    public void setEnabled(boolean active) {

    }

    @Override
    public void stateAttached(AppStateManager stateManager) {

    }

    @Override
    public void stateDetached(AppStateManager stateManager) {

    }

    @Override
    public void render(RenderManager rm) {

    }

    @Override
    public void update(float tpf) {

    }
    @Override
    public void bind(Nifty nifty, Screen screen) {
        // not used
    }

    @Override
    public void onEndScreen() {
        // not used
    }

    @Override
    public void onStartScreen() {
        // not used
    }

    @Override
    public void onEvent(String string, MenuItemActivatedEvent t) {

    }

    //**************
    // Class methods
    //**************

    /**
     * Called when the play button is clicked.
     */
    public void playButton() {

    }

    /**
     * Called when the high scores button is clicked.
     */
    public void highScoresButton() {

    }

    /**
     * Called when the settings button is clicked.
     */
    public void settingsButton() {

    }
    public void quitButton() {
        showDialog();
    }

    /**
     * Called when the rate button is clicked. Only Available on mobile.
     */
    public void rateButton() {

    }

    /**
     * Called when the feedback button is clicked. Only on mobile devices.
     */
    public void feedbackButton() {

    }

    /**
     * Called when the help button is clicked.
     */
    public void helpButton() {

    }

    /**
     * Called when the dialog needs to be shown.
     */
    public void showDialog() {
        System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
        popup = new Nifty().createPopup("popup");
        System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
        //Menu myMenu = popup.findNiftyControl("#menu", Menu.class);
        //myMenu.setWidth(new SizeValue("100px")); // must be set
        //myMenu.addMenuItem("Click me!", new menuItem("menuItemid", "blah blah")); // menuItem is a custom class
        //mNifty.subscribe(mNifty.getCurrentScreen(), myMenu.getId(), MenuItemActivatedEvent.class, this);
        mNifty.showPopup(mNifty.getCurrentScreen(), popup.getId(), null); 
    }

    public void clsoseDialog() {

    }

    /**
     * Used to return a String to the Nifty xml file.
     * 
     * @param name The name key associated with the String.
     * @return The String associated with the key.
     */
    public String getString(String name) {
        if (name.equals("play")) {
            return Strings.PLAY_BUTTON;
        }
        else if (name.equals("high_score")) {
            return Strings.HIGH_SCORES_BUTTON;
        }
        else if (name.equals("settings")) {
            return Strings.SETTINGS_BUTTON;
        }
        else if (name.equals("quit")) {
            return Strings.QUIT_BUTTON;
        }
        else if (name.equals("rate")) {
            return Strings.RATE_BUTTON;
        }
        else if (name.equals("feedback")) {
            return Strings.FEEDBACK_BUTTON;
        }
        else if (name.equals("rules")) {
            return Strings.RULES_BUTTON;
        }
        return null;
    }
}
}

发生的事情是我单击调用该quitButton()方法的退出按钮。这很好用。这反过来又会引发showDialog()问题所在。在showDialog()方法中是这一行popup = new Nifty().createPopup("popup");,它在mNifty不应该为空的那一行。任何帮助表示赞赏。

4

1 回答 1

1

线

popup = new Nifty().createPopup("popup");

不使用mNifty。它创建一个新实例,Nifty然后调用creatPopup()这个新实例。mNifty从前面开始,您通过调用看起来像工厂方法的东西进行初始化

mNifty = mNiftyDisplay.getNifty();

Nifty获取via很可能new不会返回完全初始化的实例。由于您尚未发布代码,Nifty因此尚不清楚发生了什么。

我会仔细检查以确保创建Niftyvianew将返回一个完全初始化的实例,并且您真的想要一个新实例。

于 2012-10-23T03:41:43.847 回答