变量值变化

时间:2015-12-24 00:18:57

标签: c++ enums sdl

我的问题是一堆变量,它们在程序工作期间增加了自己,这当然不应该发生。这里有代码解释(这很多代码,所以如果你有一些建议如何缩短它,会很感激),这是游戏Mastermind,这个整个代码的想法我现在正在努力,就是这种情况,当时你不能一次选择2种相同的颜色,所以如果你点击按钮,它将跳过所有采取的颜色,一直到下一个没有采取颜色(这个MOUSE_BUTTON_UP_ [颜色]是枚举类型,如果它很重要):

void Button::handleEvent( SDL_Event* e)
{
int Switch=0;
int sprite=0;
int temp = 0;
//If mouse event happened
if( e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP )
{
    //Get mouse position
    int x, y;
    SDL_GetMouseState( &x, &y );

    //Check if mouse is in button
    bool inside = true;

    //Mouse is left of the button
    if( x < mPosition.x )
    {
        inside = false;
    }
    //Mouse is right of the button
    else if( x > mPosition.x + globals::BUTTON_WIDTH )
    {
        inside = false;
    }
    //Mouse above the button
    else if( y < mPosition.y )
    {
        inside = false;
    }
    //Mouse below the button
    else if( y > mPosition.y + globals::BUTTON_HEIGHT )
    {
        inside = false;
    }
    if(inside)
    {
        switch (e->type)
        {
        case SDL_MOUSEBUTTONDOWN:
            Switch = Button::switchingBalls();
            sprite = Button::getCurrentSprite();
            if(sprite == 8)
                mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
            if(Switch + sprite > 7)
            {
                temp = 7 - sprite;
                switch (temp)
                {
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_GREY;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DBLUE;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                }
                break;
            }
            if(Switch == 0)
            {
                switch (sprite)
                {                   
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;
                    break;
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DBLUE;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_GREY;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DGREEN;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LBLUE;
                    break;
                case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 6:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
                }
                break;
            }
            else if(Switch == 1)
            {
                switch(sprite)
                {
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DBLUE;
                    break;
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_GREY;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DGREEN;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LBLUE;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
                case 6:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                }
                break;
            }
            else if(Switch == 2)
            {
                switch(sprite)
                {
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_GREY;
                    break;
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DGREEN;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LBLUE;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
                case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                case 6:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;
                    break;
                }
                break;
            }
            else if(Switch == 3)
            {
                switch(sprite)
                {
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DGREEN;
                    break;
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LBLUE;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;
                    break;
                case 6:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DBLUE;
                    break;
                }
                break;
            }
            else if(Switch == 4)
            {
                switch(sprite)
                {
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LBLUE;
                    break;
                case 1:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 2:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
                case 3:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                case 4:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;
                    break;
                case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_DBLUE;
                    break;
                case 6:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_GREY;
                    break;
                }
                break;
            }
            break;
        }
    }
}
}

Button :: switchingBalls()并不重要,没有问题。 Button :: getCurrentSprite()返回当前选定的颜色(它是枚举类型中int的值)。

现在,我在main中有4个类“Button”对象,我检查每个对象是否有变化(有人点击它)然后发生上面的整个代码。 但是,无论当前选择哪种颜色(默认情况下将类构造函数定义为RED),它总是更改为黄色。 更有趣的是,当我切换手表,并试图弄清楚会发生什么时,一切正常。如果我将断点设置为此行:

                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_RED;
                    break;
                }
                break;
            }
            if(Switch == 0)//breakpoint goes here
            {
                switch (sprite)
                {                   
                case 0:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_VIOLET;

一切正常,它工作正常('精灵'值为0)。但是,如果我将断点设置为这一行:

                    case 5:
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_LGREEN;
                    break;
                case 6://breakpoint goes here
                    mCurrentSprite = BUTTON_SPRITE_MOUSE_UP_YELLOW;
                    break;
一切都变得疯狂。 'sprite'值突然增加到6.更令人抓狂的是,当我在'case 5'之前设置断点,'case 4'等时,它发生的一切都是相同的,但是那时的值是5和4。

有人能告诉我,这件事到底是怎么回事,我真的很绝望。我没有其他想法,如何编程,或者有什么问题。帮助!

1 个答案:

答案 0 :(得分:0)

除去
int sprite=0;

并改变
sprite = Button::getCurrentSprite();

const int sprite = Button::getCurrentSprite();
并看看这会带你去哪里