1

我把这个东西作为我程序的一部分。语言是 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
                }   
            }
        }

    }
4

1 回答 1

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));
    }
}
于 2013-05-23T16:52:02.533 回答