Switch statement not working?

closed account (jw6XoG1T)
so in my game engine that im making there is a part in the program that makes an object of the Player class render and animate depending on what the Movement state is (i.e standing, running, jumping, crouching). However when i use a switch statement to check which state is active it basically skips over everything and goes to "running"...making a continuous running sprite animation. I really dont see why it would do that.

Here are the 3 main files used in this problem:


//Entity.h
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

#include "Engine.h"

namespace DarkMatter
{
        class Entity
        {

        protected:

                LPDIRECT3DTEXTURE9 texture;
                D3DXVECTOR3 pos;

                int health;
                int frame;
                int starttime;
                int width;
                int height;

        public:

                Entity();
                ~Entity();

                void LoadTexture(string filename, D3DCOLOR trans);

                void DrawFrame(int width, int height, int columns);
                void Animate(int startframe, int endframe, int direction, int delay);

                void SetPos(float x, float y);
                void SetDimensions(string filename);

                virtual void Move() = 0;
                virtual void Update() = 0;

        };

        class Player : public Entity
        {

        private:

                int health;
                int frame;
                int starttime;
                int width;
                int height;

                enum Movement {standing, running, jumping, crouching};
                Movement state;

        public:

                Player();
                ~Player();

                void Move();
                void Update();

        };
};



//Entity.cpp

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


#include "Entity.h"

namespace DarkMatter
{
        Entity::Entity()
        {
                this -> texture = NULL;
                this -> pos.x = 0; this -> pos.y = 0;

                this -> health = 0;
                this -> frame = 0;
                this -> starttime = 0;
                this -> width = 0;
                this -> height = 0;

        }

        Entity::~Entity()
        {

        }

        void Entity::LoadTexture(string filename, D3DCOLOR trans)
        {
                D3DXIMAGE_INFO info;
                D3DXGetImageInfoFromFile(filename.c_str(), &info);

                D3DXCreateTextureFromFileEx(
                        engine -> GetDevice(),
                        filename.c_str(),
                        info.Width, info.Height,
                        1,
                        D3DPOOL_DEFAULT,
                        D3DFMT_UNKNOWN,
                        D3DPOOL_DEFAULT,
                        D3DX_DEFAULT,
                        D3DX_DEFAULT,
                        trans,
                        &info,
                        NULL,
                        &this -> texture);
        }

        void Entity::DrawFrame(int width, int height, int columns)
        {
                D3DXVECTOR3 pos(this -> pos.x, this -> pos.y, 0);
                D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255);

                RECT rect;
                rect.left = (this -> frame % columns) * width;
                rect.top = (this -> frame / columns) * height;
                rect.right = rect.left + width;
                rect.bottom = rect.top + height;

                engine -> GetSprite() -> Draw(this -> texture, &rect, NULL, &pos, white);
        }

        void Entity::Animate(int startframe, int endframe, int direction, int delay)
        {
                if ((int)GetTickCount() > this -> starttime + delay)
                {
                        this -> starttime = GetTickCount();

                this -> frame += direction;
                
                if (this -> frame > endframe) this -> frame = startframe;
                if (this -> frame < startframe) this -> frame = endframe;
                
                }
        }

        void Entity::SetPos(float x, float y)
        {
                this -> pos.x = x;
                this -> pos.y = y;
        }

        void Entity::SetDimensions(string filename)
        {
                D3DXIMAGE_INFO info;
                D3DXGetImageInfoFromFile(filename.c_str(), &info);

                this -> width = info.Width;
                this -> height = info.Height;
        }
};



//Player.cpp

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



#include "Entity.h"

namespace DarkMatter
{
        Player::Player()
        {
                this -> health = 0;
                this -> frame = 0;
                this -> starttime = 0;
                this -> width = 37;
                this -> height = 44;

                this -> state = standing;

        }

        Player::~Player()
        {

        }

        void Player::Move()
        {
                engine -> GetKeyboard() -> Acquire();
                engine -> GetKeyboard() -> GetDeviceState(sizeof(engine -> keys), (LPVOID)&engine -> keys);

                if (engine -> keys[DIK_LEFT] & 0x80)
                {
                        this -> pos.x -= 3.0f;
                        this -> state = running;
                }

                if (engine -> keys[DIK_RIGHT] & 0x80)
                {
                        this -> pos.x += 3.0f;
                        this -> state = running;
                }

                if (engine -> keys[DIK_UP] & 0x80)
                {
                        this -> pos.y -= 3.0f;
                        this -> state = standing;
                }

                if (engine -> keys[DIK_DOWN] & 0x80)
                {
                        this -> pos.y += 3.0f;
                        this -> state = standing;
                }

                else 
                {
                        this -> frame = 0;
                        this -> state = standing;
                }

                if (engine -> keys[DIK_ESCAPE] & 0x80)
                {
                        gameover = true;
                }
        }

        void Player::Update()
        {
                this -> Move();

                switch (this -> state)
                {
                case standing:
                        {
                                this -> DrawFrame(37, 44, 6);
                        }

                case running:
                        {
                                this -> DrawFrame(37, 44, 6);
                                this -> Animate(0, 5, 1, 60);
                        }
                }
        }
};




As you can see i made an Enum in the Player class that holds all the possible movement types. Then when the player presses the keys to make the sprite move the state changes from standing to running. In the Update() function it uses a switch statement to check which state the character is in to make the corrisponding animation. However it jumps straight to "case running" and makes a continuous running animation. If you could help me out then thanks :)
closed account (DSLq5Di1)
You need to use break after each case to stop execution from falling through to the next label,
1
2
3
4
5
    case standing:
        {
            this -> DrawFrame(37, 44, 6);
            break;
        }
your making a game engine- you should know how a simple switch statement works
add break after each case or execution falls through to next case
closed account (jw6XoG1T)
dont you think i knew that? I added those before but they just stopped the animation from working right for some reason...it would just get stuck in one state even though the movement keys were being pressed
dont you think i knew that?


Given that you removed the break statements, and then had to ask us why the code always went to "running"...
closed account (jw6XoG1T)
lol believe me i had them in there before...i just took them out because i saw they werent doing what i wanted them to do...im still working on this problem right now but i really dont know whats wrong with it :(
Topic archived. No new replies allowed.