Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

23.12.2012, 00:06

Problem mit Tastendruck

Wenn ich Enter während des Gameoverscreens drücke, komme ich nur in den MenuState, wenn ich in folgender Zeile der Menu-Klasse nicht Enter als Taste angebe.
Mit einer anderen Taste(zum Beispiel Q) funktioniert es:

C#-Quelltext

1
if (kbState.IsKeyDown(Keys.Q))game1.ChangeGameState(Game1.GameStates.MaingameState);

Mit Enter funktioniert es nicht. Dann komme ich nicht ins Menü, sondern ins Maingame.

C#-Quelltext

1
if (kbState.IsKeyDown(Keys.Enter))game1.ChangeGameState(Game1.GameStates.MaingameState);

Meine Vermutung:
Wenn ich während des Gameoversreens Enter drücke, wechselt das Spiel zuerst auf den MenuState, und danach sofort auf den MaingameState, weil ich in der Menü-Klasse durch Enter drücken ins Maingame wechsele. Deshalb müssen sich die Tasten unterscheiden damit nicht automatisch ein State übersprungen wird.
Jetzt meine Frage:
Wie kann ich das Problem umgehen? Sagen wir mal ich möchte alles mit Enter machen. Also das switchen vom GameoverState auf den MenuState und das switchen vom MenuState auf den MaingameState.
Wie geht man am besten vor wenn man möchte dass pro Tasten-Druck nur eine if-Abfrage ausgeführt wird? Und nicht noch eine andere if-Abfrage in einer anderen Klasse.
Ich habe versucht das Keyboard-Problem durch einen oldState/newState zu lösen, jedoch ohne Erfolg.

C#-Quelltext

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
102
103
104
105
106
107
108
109
110
public class Game1 : Microsoft.Xna.Framework.Game     { 
        GraphicsDeviceManager graphics; 
        SpriteBatch spriteBatch; 
        IState lastState, currentState; 

        public enum GameStates 
        { 
            IntroState = 0, 
            MenuState = 1, 
            MaingameState = 2, 
            IngamemenuState = 3, 
            GameoverState = 4 
        } 

        public void ChangeGameState(GameStates newState) 
        { 
            if (newState == currentGameState) 
                return; 
            lastGameState = currentGameState; 
            lastState = currentState; 
            switch (newState) 
            { 
                case GameStates.IntroState: 
                    currentState = new Intro(this); 
                    currentGameState = GameStates.IntroState; 
                    break; 
                case GameStates.MenuState: 
                    currentState = new Menu(this); 
                    currentGameState = GameStates.MenuState; 
                    break; 
                case GameStates.MaingameState: 
                      currentState = new Maingame(this); 
                      currentGameState = GameStates.MaingameState; 
                    break; 
                case GameStates.IngamemenuState: 
                    currentState = new Ingamemenu(this); 
                    currentGameState = GameStates.IngamemenuState; 
                    break; 
                case GameStates.GameoverState: 
                    currentState = new Gameover(this);              
                    currentGameState = GameStates.GameoverState; 
                    break; 
            } 
            currentState.Load(Content); 
        } 

        public void ChangeCurrentToLastGameState() 
        { 
            currentGameState = lastGameState; 
            currentState = lastState; 
        } 

        public GameStates CurrentState 
        { 
            get { return currentGameState; } 
            set { currentGameState = value; } 
        } 

        public GameStates LastState 
        { 
            get { return lastGameState; } 
            set { lastGameState = value; } 
        } 

        private GameStates currentGameState = GameStates.IntroState; 
        private GameStates lastGameState; 
      
        public Game1() 
        { 
            graphics = new GraphicsDeviceManager(this); 
            Content.RootDirectory = "Content"; 
        } 
        
        protected override void Initialize() 
        { 
            currentState = new Intro(this); 
            currentGameState = GameStates.IntroState; 
            base.Initialize(); 
        } 

        protected override void LoadContent() 
        {        
            spriteBatch = new SpriteBatch(GraphicsDevice); 
            currentState.Load(Content); 
        } 

        protected override void Update(GameTime gameTime) 
        { 
            currentState.Update(gameTime); 
            if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState)) 
            { 
                lastState.Update(gameTime); 
            } 
            base.Update(gameTime); 
        } 

        protected override void Draw(GameTime gameTime) 
        { 
            GraphicsDevice.Clear(Color.CornflowerBlue); 
            spriteBatch.Begin(); 
            if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState)) 
            { 
              lastState.Render(spriteBatch); 
            } 
            currentState.Render(spriteBatch); 
            spriteBatch.End(); 
            
            base.Draw(gameTime); 
        } 
    } 


C#-Quelltext

1
2
3
4
5
public interface IState         { 
            void Load(ContentManager content); 
            void Update(GameTime gametime); 
            void Render(SpriteBatch batch); 
        } 

C#-Quelltext

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
public class Intro : IState     { 
        Texture2D Titelbildschirm; 
        private Game1 game1;        

        public Intro(Game1 game) 
        { 
            game1 = game; 
        } 

        private KeyboardState oldState; 

        public void Load(ContentManager content) 
        { 
           Titelbildschirm = content.Load<Texture2D>("gruft"); 
        } 

        public void Update(GameTime gametime) 
        { 
             KeyboardState newState = Keyboard.GetState();  
            if(oldState.IsKeyUp(Keys.Space) && newState.IsKeyDown(Keys.Space)) 
                game1.ChangeGameState(Game1.GameStates.MenuState); 

            oldState = newState;  
        } 

        public void Render(SpriteBatch batch) 
        { 
            batch.Draw(Titelbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); 
        } 
    } 


C#-Quelltext

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
public class Menu : IState     { 
        Texture2D Choosescreen; 
        private Game1 game1; 

        public Menu(Game1 game) 
        { 
            game1 = game; 
        } 

        private KeyboardState oldState; 

        public void Load(ContentManager content) 
        { 
            Choosescreen = content.Load<Texture2D>("menubild"); 
        } 

        public void Update(GameTime gametime) 
        { 
            KeyboardState newState = Keyboard.GetState();  
            if(oldState.IsKeyUp(Keys.Enter) && newState.IsKeyDown(Keys.Enter)) 
                game1.ChangeGameState(Game1.GameStates.MaingameState); 
             if(oldState.IsKeyUp(Keys.Escape) && newState.IsKeyDown(Keys.Escape)) 
                game1.Exit(); 

            oldState = newState;  
        } 

        public void Render(SpriteBatch batch) 
        { 
            batch.Draw(Choosescreen, new Rectangle(0, 0, 1280, 720), Color.White); 
        } 
    } 


C#-Quelltext

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
public class Maingame : IState     { 
        Texture2D Spielbildschirm, axe; 
        Vector2 position = new Vector2(100,100); 

        private Game1 game1; 

        public Maingame(Game1 game) 
        { 
            game1 = game; 
        } 

        private KeyboardState oldState; 

        public void Load(ContentManager content) 
        { 
            Spielbildschirm = content.Load<Texture2D>("hauszombie"); 
            axe = content.Load<Texture2D>("axxx"); 
        } 

        public void Update(GameTime gametime) 
        { 
              KeyboardState newState = Keyboard.GetState(); 

              float delta = (float)gametime.ElapsedGameTime.TotalSeconds; 
              position.X += 5 * delta; 
              position.Y += 3 * delta; 

              if (oldState.IsKeyUp(Keys.Escape) && newState.IsKeyDown(Keys.Escape)) 
                game1.ChangeGameState(Game1.GameStates.IngamemenuState); 
              if (oldState.IsKeyUp(Keys.A) && newState.IsKeyDown(Keys.A)) 
                game1.ChangeGameState(Game1.GameStates.GameoverState); 

              oldState = newState;  
        } 

        public void Render(SpriteBatch batch) 
        { 
            batch.Draw(Spielbildschirm, new Rectangle(0, 0, 1280, 720), Color.White); 
            batch.Draw(axe, position, Color.White); 
        } 
    } 


C#-Quelltext

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
public class Ingamemenu : IState     { 
        Texture2D Quitscreen; 
        private Game1 game1; 

        public Ingamemenu(Game1 game) 
        { 
            game1 = game; 
        } 

        private KeyboardState oldState; 

        public void Load(ContentManager content) 
        { 
            Quitscreen = content.Load<Texture2D>("quit"); 
        } 

        public void Update(GameTime gametime) 
        { 
            KeyboardState newState = Keyboard.GetState();  
            if (oldState.IsKeyUp(Keys.Y) && newState.IsKeyDown(Keys.Y)) 
               game1.ChangeGameState(Game1.GameStates.MenuState); 

            if (oldState.IsKeyUp(Keys.N) && newState.IsKeyDown(Keys.N)) 
              game1.ChangeCurrentToLastGameState(); 

            oldState = newState; 
        } 

        public void Render(SpriteBatch batch) 
        { 
            batch.Draw(Quitscreen, new Rectangle(200, 200, 200, 200), Color.White); 
        } 
    } 


C#-Quelltext

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
public class Gameover : IState     { 
        Texture2D Gameoverscreen; 

        private Game1 game1; 

        public Gameover(Game1 game) 
        { 
            game1 = game; 
        } 

        private KeyboardState oldState; 

        public void Load(ContentManager content) 
        { 
            Gameoverscreen = content.Load<Texture2D>("gameover"); 
        } 

        public void Update(GameTime gametime) 
        { 
            KeyboardState newState = Keyboard.GetState();  
            if(oldState.IsKeyUp(Keys.Enter) && newState.IsKeyDown(Keys.Enter)) 
                game1.ChangeGameState(Game1.GameStates.MenuState); 

            oldState = newState;  
        } 

        public void Render(SpriteBatch batch) 
        { 
            batch.Draw(Gameoverscreen, new Rectangle(0, 0, 1280, 720), Color.White); 
        } 
    } 

NachoMan

Community-Fossil

Beiträge: 3 885

Wohnort: Berlin

Beruf: (Nachhilfe)Lehrer (Mathematik, C++, Java, C#)

  • Private Nachricht senden

2

23.12.2012, 00:13

Der selbe Tastendruck wird im Menü auch behandelt, was dazu führt, dass du gleich weiter zum Spiel geleitet wirst.
Die Ursache hab ich innerhalb von 5 Sekunden mit Hilfe der Suchfunktion des Browsers gefunden. Gibt dir das nächste mal bitte mehr Mühe bei der Fehlersuche bevor du einen Thread eröffnest.
"Der erste Trunk aus dem Becher der Erkenntnis macht einem zum Atheist, doch auf dem Grund des Bechers wartet Gott." - Werner Heisenberg
Biete Privatunterricht in Berlin und Online.
Kommt jemand mit Nach oMan?

Schorsch

Supermoderator

Beiträge: 5 145

Wohnort: Wickede

Beruf: Softwareentwickler

  • Private Nachricht senden

3

23.12.2012, 03:55

Das selbe habe ich dir doch in dem anderen Thread schon gesagt.
Es ist ganz einfach. Dort steht

C#-Quelltext

1
if (kbState.IsKeyDown(Keys.Enter))game1.ChangeGameState(Game1.GameStates.MaingameState);

Nun was heißt das. Wenn die Entertaste gedrückt wird, dann wechsle den GameState auf MaingameState. Wenn du in deinem GameoverState bist drückst du Enter und wechselst in den MenuState. Im MenuState hälst du die Entertaste ja immer noch gedrückt also wechselt du weiter in den nächsten State. Du musst also versuchen, dass beim ersten if gewechselt wird und beim zweiten nicht. Also muss die Bedingung beim zweiten if false sein. Die Update Funktion wird einmal pro Frame aufgerufen. Du willst also nur in dem Frame wissen, in welchem Enter runter gedrückt wird und nicht die folgenden. Du möchtest nur den ersten Frame. Wenn du dir jetzt den letzten Status von der Tastatur speicherst und den aktuellen abfragst, dann kannst du diesen Frame bestimmen. Wenn im letzten Frame die Entertaste nicht gedrückt wurde, aber im aktuellen, dann möchtest du wechseln. Wenn im letzten Frame die Entertaste gedrückt wurde, im aktuellen aber nicht, dann willst du nicht wechseln. Du willst nur den Moment des Drückens.

C#-Quelltext

1
2
KeyboardState lastState;
KeyboardState currentState;

Im Konstruktor oder irgendwo am Anfang musst du currentState etwas zuweisen.

C#-Quelltext

1
currentState = Keyboard.GetState;


Und anstatt dann über Keyboard.GetState() deine Tasten abzufragen, benutzt du deine currentState und lastState Member.
Einmal pro Frame musst du updaten:

C#-Quelltext

1
2
lastState = currentState;
currentState = Keyboard.GetState();

Und wenn du dann wissen willst, ob die Taste grad in diesem Moment runter gedrückt wurde, dann fragst du das so ab:

C#-Quelltext

1
if(currentState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter)) game.ChangeState(game.Menu);

Musst du natürlich noch ein bisschen anpassen, aber das Prinzip sollte klar sein. Buch ist bestellt?;)
„Es ist doch so. Zwei und zwei macht irgendwas, und vier und vier macht irgendwas. Leider nicht dasselbe, dann wär's leicht.
Das ist aber auch schon höhere Mathematik.“

4

23.12.2012, 20:17

Danke. Es funktioniert jetzt. Ich werde das Buch erst die nächsten Tage bestellen.

Werbeanzeige