Lua Love2d 写代码实例一次,多次使用,每个实例都是独一无二的。
2016-1-3 17:51:17
收藏:0
阅读:85
评论:1
我曾经问过一个类似的问题,但那是关于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
但是,当我右键单击以添加圆形(增加循环运行的次数)时,它没有为我添加第二个圆形,以便我可以独立于第一个圆形移动。请帮忙?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
我处理多个椭圆的方式是为每个椭圆创建一个表格,该表格保存它的属性和一个
draw函数。local ellipses = {} -- 将所有椭圆存储在一个表中 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 -- 将新的椭圆插入到椭圆表中 return ellipse end function love.draw() for i = 1,#ellipses do ellipses[i].draw(); -- 调用每个椭圆的独立draw函数 end end function love.mousereleased(x,y,button) if button == 2 then create_ellipse(x,y,50,50,255,0,0) -- 奖励:每个椭圆都是在用户单击的位置创建的 end end function love.update(dt) if love.mouse.isDown(1) then local mousex,mousey = love.mouse.getPosition() -- 没有必要每帧都请求鼠标位置,只需要当用户在屏幕上的任何位置点击时 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,甚至可以自己创建一个椭圆类。