【问题标题】:Lua Love2d Write Code Instance Once, Use Multiple Times With Each One Being UniqueLua Love2d 编写代码实例一次,多次使用,每次唯一
【发布时间】:2016-04-07 09:15:46
【问题描述】:

我问过一个与此类似的问题,但我问的是 Processing.JS。现在我正在用 LOVE2D Lua 制作一些东西,我想做的是我有一个按钮,当点击它时,它会在屏幕上添加一个圆圈。我已经有代码,当我单击并按住圆圈时,我可以移动它。但是当我添加第二个圆圈时,他们都使用相同的变量。我想要它,所以我编写了一次代码实例来移动和添加圆圈,但我可以多次调用它并且每个都是唯一的,而无需编写代码来预测无限数量的圆圈。这是我的代码:

obn = 0
ellipsex = 50
ellipsey = 50
ellipsew = 50
ellipseh = 50
ellipser = 255
ellipseg = 0
ellipseb = 0
function love.draw()
mousex, mousey = love.mouse.getPosition()
for i=0,obn,1 do
ellipse()
end
end


function love.mousereleased(x, y, button)
   if button == 2 then
      obn = obn + 1
   end

end




function love.update(dt)
    if love.mouse.isDown(1) then
           if mousex > ellipsex and mousex < ellipsex + ellipsew and mousey > ellipsey and mousey < ellipsey + ellipseh then
ellipsex = mousex
ellipsey = mousey
end
    end
end





function ellipse()






love.graphics.setColor(ellipser, ellipseg, ellipseb)
love.graphics.ellipse("fill", ellipsex, ellipsey, ellipsew, ellipseh)

end

但是当我右键单击添加一个圆圈(增加 for 循环运行的次数)时,它不会添加第二个圆圈让我独立于第一个圆圈移动。帮忙?

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    我处理多个椭圆实例的方法是为每个椭圆创建一个表格,其中包含其属性和draw 函数。

    local ellipses = {} -- store all ellipses into a table
    
    function create_ellipse(x,y,w,h,r,g,b)
        local ellipse = {
            x = x,
            y = y,
    
            w = w,
            h = h,
    
            r = r,
            g = g,
            b = b
        }
    
        function ellipse.draw()
            love.graphics.setColor(ellipse.r,ellipse.g,ellipse.b)
            love.graphics.ellipse("fill",ellipse.x,ellipse.y,ellipse.w,ellipse.h)
        end
    
        ellipses[#ellipses+1] = ellipse -- insert new ellipse into ellipses table
    
        return ellipse
    end
    
    function love.draw()
        for i = 1,#ellipses do
            ellipses[i].draw(); -- call each ellipse's separate draw function
        end
    end
    
    function love.mousereleased(x,y,button)
        if button == 2 then
            create_ellipse(x,y,50,50,255,0,0) -- bonus: every ellipse is created where the user clicked
        end
    end
    
    function love.update(dt)
        if love.mouse.isDown(1) then
            local mousex,mousey = love.mouse.getPosition() -- there is no need to request the mouse position every frame, but only when a user clicks anywhere on the screen
    
            for i = 1,#ellipses do
                local current_ellipse = ellipses[i]
    
                if mousex >= current_ellipse.x and mousex <= current_ellipse.x+current_ellipse.w and mousey >= current_ellipse.y and mousey <= current_ellipse.y+current_ellipse.h then
                    current_ellipse.x = mousex
                    current_ellipse.y = mousey
                end
            end
        end
    end
    

    如果您喜欢 OOP,您甚至可以创建自己的 Ellipse 类。

    【讨论】:

    • 我在运行它时遇到了一个错误,当我右键单击并制作一个椭圆然后尝试移动它时,我得到了这个:prntscr.com/9lpal7
    • 我的另一个错误.. 我在绘图函数中使用了错误的(静态)变量,所以即使对象的位置得到更新,它也只是被忽略了。对不起。我又编辑了一遍。
    • 这行得通!非常感谢:D,这是让我无法完成项目的主要问题。我也会看到你对“独立对象创建”的贡献,我喜欢在值得称赞的地方给予信任。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2014-12-07
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多