【问题标题】:Issues while making some kind of menu制作某种菜单时的问题
【发布时间】:2013-05-23 16:00:49
【问题描述】:

我把这个东西作为我程序的一部分。语言是 C++,带有 Allegro 库。我想让它做下一个:单击时,两个矩形之一周围会出现一个边框。

它会发生,但只有一次,在开始时。之后,当我将鼠标移开时,边框一直消失。此外,在我单击的任何位置,边框都会出现在正确的位置。

mouseX 和 mouseY 完美运行,即使数字相同。但是该动作仅在我想要的方式上发生一次。点击时如何将其扩展到每个案例?

    if(asd.type == ALLEGRO_EVENT_MOUSE_AXES)
    {
        mouseX = asd.mouse.x;
        mouseY = asd.mouse.y;
    }

    if(asd.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
    {
        if(asd.mouse.button & 1)
        {
            if (mouseX > 592 && mouseX < 608 && mouseY > 540 && mouseY < 556)
            {
                Chosen_Cell = 1;
                borderX = 592;
                borderY = 540;
            }

            if (mouseX > 592 && mouseX < 608 && mouseY > 556 && mouseY < 562)
            {
                Chosen_Cell = 2;
                borderX = 592;
                borderY = 556;
            }

            al_draw_rectangle(borderX, borderY, borderX + 16, borderY + 16, al_map_rgb(255, 255, 0),2);

            if (16 < mouseX && mouseX < 528 && 16 < mouseY &&mouseY < 736)
            {
                switch (Chosen_Cell)
                {
                                        //blahblah, not important
                }   
            }
        }

    }

【问题讨论】:

    标签: c++ menu mouse allegro allegro5


    【解决方案1】:

    边框正在消失,因为您在按钮按下事件中调用了绘图代码。这意味着一旦释放按钮,矩形将不再显示。通常更好的做法是让事件更新值,然后在事件队列为空并由计时器调用重绘时绘制屏幕上的所有内容。

    //setup a timer and a redraw var
    ALLEGRO_TIMER *timer;
    bool redraw = false;
    
    timer = al_create_timer(1.0/FPS);
    
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_start_timer(timer);
    
    while(running)
    {
        if(asd.type == ALEGRO_EVENT_TIMER)
            redraw = true;
        if(asd.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
        {
            if(asd.mouse.button & 1)
            {
               if (mouseX > 592 && mouseX < 608 && mouseY > 540 && mouseY < 556)
               {
                   Chosen_Cell = 1;
                   borderX = 592;
                   borderY = 540;
               }
    
               if (mouseX > 592 && mouseX < 608 && mouseY > 556 && mouseY < 562)
               {
                   Chosen_Cell = 2;
                   borderX = 592;
                   borderY = 556;
               }
           } 
        if(redraw && al_is_event_queue_empty(asd))
        {
            al_draw_rectangle(borderX, borderY, borderX + 16, borderY + 16, al_map_rgb(255, 255, 0),2);
            redraw = false;
            al_flip_display();
            al_clear_to_color(al_map_rgb(0, 0, 0));
        }
    }
    

    【讨论】:

    • asd 是一个事件,所以我认为条件是al_is_event_queue_empty(_name of the event queue_)
    • 无论如何,它似乎不起作用:我唯一能看到的就是闪光灯。另外,这种方法会使程序的其他部分运行起来很慢。
    • @ZoltánSchmidt:如果它不起作用...... 你为什么接受它
    • 因为这个问题已经不重要了,但我不能删除,因为它已经有了答案。
    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2014-06-10
    相关资源
    最近更新 更多