在 Ruby Gosu 库中切换 Draw 函数

问题描述 投票:0回答:1

我目前正在努力显示将链接到鼠标左键按下的 font.draw 元素列表。我已将绘制设置为如果按钮按下并且 mouse_x/y 位于正确的位置,但是一旦您停止按下鼠标左键,它们就会消失,这是有道理的。

我想知道是否有一种方法可以在鼠标按下时设置/切换这些绘图,我一直在努力寻找任何资源或示例来显示这一点,并且在本学期的工作中从未教授过它。

这是当前设置的代码;

def draw
        music_file = File.new("albums.txt", "r")
        albums = read_albums(music_file)
        artworks = read_artworks(albums)
        music_file.close() 
        @info_font.draw("mouse_x: #{mouse_x}", 0, 590, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) #debug
        @info_font.draw("mouse_y: #{mouse_y}", 100, 590, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) #debug
        draw_background() 
        draw_albums(albums)
        drawtracks_main(albums) #this is the issue I'm trying to fix
    end
    def drawtracks_main(albums)
        if (Gosu.button_down? Gosu::MsLeft and mouse_over_album1(mouse_x, mouse_y))
            draw_tracks(albums[0].tracks) == true
        end
    end
def draw_tracks(tracks)
        count = tracks.length
        index = 0
        ypos = 19
        xpos = 585
        while index < count
            @track_font.draw((tracks[index].name), xpos, ypos, ZOrder::UI, 1.0, 1.0, Gosu::Color::BLACK) 
            index += 1
            ypos += 20
        end
    end
    def mouse_over_album1(mouse_x, mouse_y)
        if ((mouse_x > 19 and mouse_x < 287) and (mouse_y > 19 and mouse_y < 287))
            true
        else false
        end
    end

我尝试将其切换为在button_down内绘制,但我注意到你实际上无法在button_down中进行draw。否则,我尝试将绘图设置为 true,尝试给它一个值,但这当然没有奏效。

任何帮助都将是巨大的!

ruby-on-rails ruby draw gosu libgosu
1个回答
0
投票

所以坐在那里盯着墙壁看了一个小时后,我意识到我应该使用 gosu 中的内置更新程序。

将 @draw_tracks_enabled 值初始化为 false,然后使用它;; 默认更新 if Gosu.button_down?(Gosu::MsLeft) if mouse_over_album1(mouse_x, mouse_y) @draw_tracks1_enabled = true 别的 @draw_tracks1_enabled = false 结尾 结尾 将其变为真(如果您要按要显示的另一个项目,则将其变为假)。然后,您可以编写一个简单的 if @draw_tracks1_enabled == true 然后绘制轨道。

© www.soinside.com 2019 - 2024. All rights reserved.