无法正常动态加载和设置游戏状态

时间:2015-06-01 00:26:27

标签: java swing

好吧,这是一个复杂的问题,所以让我们尝试启动它 所以,我相信很多人都玩过游戏,他们会显示带有保存文件标识的按钮,当你按下它时,它会从该文件中检索保存状态并开始播放 我想为一个简单的RPG实现类似的东西(想想简化的火焰徽章,如果你正在玩它)。 所以我所做的是设置一个带有按钮的jpanel,其中包含actioncommand"加载并播放保存文件",这是我尝试在actionlistener中使用的代码来执行该功能 rpggame是一个jpanel,包含我的相关字符,光标等,setgame是一种帮助设置它的方法 (使用按钮文本是尝试检索特定文件) 使用+" .dat"是试图获得序列化状态

else if (e.getActionCommand().equals("load and play saved file")) {
    JButton source = (JButton)(e.getSource());
    try {
        newgame = loadChapterState(source.getText() + ".dat");
        newgame.SetUpGame();
        //newgame.setCursor(newgame.cursor);
        CardLayout layout = (CardLayout)(overallgame.getLayout());
        layout.show(overallgame, GAME);
        //requestFocusInWindow
        newgame.requestFocus /*InWindow*/ (true);

        dummyframe.repaint();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(dummyframe, "Darn it!", "Error in retrieval", JOptionPane.ERROR_MESSAGE);
        //ex.printStackTrace();
    }​

这是loadChapterState(游戏状态加载)方法的代码

​       public static RPGGame loadChapterState(String file) throws IOException {
        ObjectInputStream ois = null;
        RPGGame gamedata = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            gamedata = (RPGGame) ois.readObject();
            gamedata.setCursor(gamedata.cursor);
        } catch (IOException ex) {
            System.out.println("Sorry, there was an error reaching the file");
            System.out.println("You will not be able to play with this state");
            //ex.printStackTrace();
        } finally {
            ois.close();
            return gamedata;
        }
    }
让我感到困惑的是,当我运行它时,我完全得到了游戏地图的图像,但我实际上无法通过输入到keylistener来移动光标,尽管在上面的加载方法中将其重置为setCursor方法(我刚刚获得该游戏地图的任何旧副本时遇到了麻烦)。

现在,我确实稍后在我的main方法中加载了保存状态,就像这样。

System.out.println("Would you like to create a file?");
 try {
     newgame = loadChapterState("stuff.dat");
     newgame.SetUpGame();
 } catch (IOException e) {
     e.printStackTrace();
 }

 //finally constructs master panel that can flip between main menu and running game
 overallgame = new JPanel(new CardLayout());
 overallgame.add(mainmenupanel, MAIN_MENU);
 overallgame.add(newgame, GAME);
 overallgame.add(creditpanel, CREDITS);
 overallgame.add(chapterstartmenupanel, CHAPTERSTART);
 overallgame.add(showsavefilespanel, SAVE_FILES);​

(这个可以完美地使用光标,将我的actionlistener的另一部分与下一位相结合后,将该侦听器分配给带有" start" actioncommmand的JButton)

if (e.getActionCommand().equals("start")) {
    //dummyframe.add(newgame);
    //mainmenupanel=null;
    CardLayout layout = (CardLayout)(overallgame.getLayout()); //retrieves the overall layout
    layout.show(overallgame, GAME); //uses it to switch to main game panel
    newgame.requestFocus(true); //need to redirect focus to the game rather than original main menu
    dummyframe.repaint();
}​

如果请求,我将显示rpggame类。 我想我想要的是,如何让我的光标正常工作?#34;加载和播放保存fie"命令? 谢谢你的帮助!

0 个答案:

没有答案