Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D Problem with state  (Read 300 times)
0 Members and 1 Guest are viewing this topic.
Offline Dicee

Junior Duke


Medals: 2



« Posted 2013-11-25 21:30:56 »

As you can see below in the code I'm overriding enter method, to make sure that everything is reset before I join the state again.
Let me take you quickly through. So you enter the Settings State, you make some changes, dialog pops up with a message "Do you want to save the game settings?",
with 3 options (YES, NO, CANCEL). You press NO, and it takes you to the Game Menu State. You come back to the settings, and BOOM, settings did not reset.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
public class SettingsScreenState extends PAbstractState {
   
    public static final int ID = 5;

    private Image titleImage;
    private PButton backButton;
    private PDialog dialog;

    private boolean saveSettingsDialog;
   
    private SettingsScreenUI settingsScreenUI;
   
    public SettingsScreenState() {}
   
    @Override
    public int getID() {
        return ID;
    }

    @Override
    public void enter(GameContainer gc, StateBasedGame sg) throws SlickException {
        settingsScreenUI = new SettingsScreenUI();
        settingsScreenUI.init(gc);
    }
   
    @Override
    public void init(GameContainer gc, StateBasedGame sg) throws SlickException {
        titleImage = new Image("res/gui/titleSettings.png");
       
        backButton = new PButton("buttonBack");
        dialog = new PDialog();
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sg, Graphics g) throws SlickException {
        PTheme.bgTheme(gc, g);
       
        float titleX = 50;
        float titleY = 50;
        titleImage.draw(titleX, titleY);
       
        float backButtonX = gc.getWidth() / 2 - backButton.getWidth() / 2;
        float backButtonY = gc.getHeight() - backButton.getHeight() - 30;
        backButton.render(g, backButtonX, backButtonY);
       
        settingsScreenUI.render(gc, g);    

        if (saveSettingsDialog) {
            String title = "Information";
            String text = "Some of your settings will not take effect until you restart the game.\n\n"
                        + "Do you want to save the game settings?";
            dialog.showConfirmDialog(gc, g, title, text);
        }
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sg, int delta) throws SlickException {
        Input input = gc.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();
       
        System.out.println("\nMessage dialog: " + dialog.getDialogResponse());
        System.out.println("Panel has focus: " + this.hasFocus);
        System.out.println("Start settings: " + Settings.getStartSettings());
        System.out.println("Current settings: " + settingsScreenUI.getCurrentSettings());
        System.out.println("Settings have been changed: " + settingsScreenUI.haveSettingsChanged());
       
        if (saveSettingsDialog) {        
            switch (dialog.getDialogResponse()) {
                case YES:
                    Settings.save();
                    saveSettingsDialog = false;
                    dialog.performAction(this);
                    sg.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
                    break;
                case NO:
                    saveSettingsDialog = false;
                    dialog.performAction(this);
                    sg.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
                    break;
                case CANCEL:
                    saveSettingsDialog = false;
                    dialog.performAction(this);
                    break;
            }
        }
       
        if (!this.hasFocus) return;
       
        if (backButton.isActionPerformed(input, mx, my)) {
            if (settingsScreenUI.haveSettingsChanged()) {
                saveSettingsDialog = true;
                dialog.performAction(this);
            } else {
                sg.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
            }
        }
        settingsScreenUI.update(gc, sg, delta);
    }

}


Below are the methods which are part of the SettingsScreenUI:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public List getCurrentSettings() {
        List currentSettings = new ArrayList(Settings.getStartSettings().size());
       
        currentSettings.add(Settings.video.getWidth());
        currentSettings.add(Settings.video.getHeight());
        currentSettings.add(Settings.video.isFullScreen());
        currentSettings.add(Settings.video.isFps());
        currentSettings.add(Settings.sound.isSound());
        currentSettings.add(Settings.sound.getVolume());
       
        return currentSettings;
    }
   
    public boolean haveSettingsChanged() {
        for (int i = 0; i < Math.max(Settings.getStartSettings().size(), getCurrentSettings().size()); i++) {
            if (!Settings.getStartSettings().get(i).equals(getCurrentSettings().get(i))) {
                return true;
            }
        }
        return false;
    }
Offline Dicee

Junior Duke


Medals: 2



« Reply #1 - Posted 2013-11-25 21:45:55 »

nvm.. i found out  :
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (46 views)
2014-10-17 03:59:02

Norakomi (37 views)
2014-10-16 15:22:06

Norakomi (28 views)
2014-10-16 15:20:20

lcass (32 views)
2014-10-15 16:18:58

TehJavaDev (62 views)
2014-10-14 00:39:48

TehJavaDev (62 views)
2014-10-14 00:35:47

TehJavaDev (52 views)
2014-10-14 00:32:37

BurntPizza (70 views)
2014-10-11 23:24:42

BurntPizza (40 views)
2014-10-11 23:10:45

BurntPizza (82 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!