如何在Lua中为随机生成的数字分配一个函数
2013-11-19 15:10:17
收藏:0
阅读:85
评论:2
首先我将10张图像存储在数组中(即表),键值从1到10,然后我使用“math.random”函数创建一个0到9之间的随机数。
我需要通过随机函数创建的值访问存储在数组中的图像,并仅为特定的图像文件分配触摸函数。
例如:
如果随机函数创建数字“5”,我需要移动存储在数组索引为5的位置的5.png图像。除了5.png之外的其他图像都不应使用触摸函数。(即,它们不允许在屏幕上移动但需要显示在屏幕上)
以下是我的代码:
local myText1 = display.newText(tostring(no1),130, 100, "Jokerman", 36);
myText1:setTextColor(238,18,137)
print("text value1 :",no1)
local myText2 = display.newText(tostring(ran),130, 140, "Jokerman", 36);
myText2:setTextColor(238,18,137)
print("text value2 :",ran)
result = no1 + ran;
print("Result is:" ,result)
local myres = result
print("myresultant string is -->" ,myres)
myres1 = myres % 10;
myres2 = math.floor(myres / 10);
print(myres1)
print(myres2)
--分配数值
dig1 = myres1
dig2 = myres2
function dig1:touch(event)
local t = event.target
-- printTouch(event)
local phase = event.phase
if phase == "began" then
-- 将目标设置为最上层的对象
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
-- 此标志是为了防止将虚假事件发送到目标
t.isFocus = true
-- 存储初始位置
t.x0 = event.x - t.x
t.y0 = event.y - t.y
-- 使我的对象暂时成为动力学
event.target.bodyType = "kinematic"
-- 停止当前的运动,如果有的话
event.target:setLinearVelocity(0,0)
event.target.angularVelocity = 0
elseif t.isFocus then
if phase == "moved" then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif phase == "ended" or phase == "cancelled" then
if currentTarget ~= nil and isHighlighted then
-- 将碎片移动到目标
transition.to(t,{
time = 150,
x = currentTarget.x,
y = currentTarget.y
})
currentTarget = nil
isHighlighted = false
end
display.getCurrentStage():setFocus(nil)
t.isFocus = false
-- 将身体类型切换回“静态”
event.target.bodyType = "static"
end
end
return true
end
dig1:addEventListener("touch",dig1)
点赞
用户869951
抱歉我还是不清楚问题。但是在评论中无法解释清楚,所以尝试用这个答案。
以下是我认为您正在尝试进行的伪代码:
-- 初始化(启动)
function touch(image)
…对图像做某些事情…
end
function noTouch(image)
end -- 没有要触摸的内容
imageArray = 10张图像的数组
imageArray中的每个图像都是“Image”类的
对于imageArray中的每个图像,做如下处理:
image.touch = noTouch -- 一个“什么都不做”的函数
-- 初始化完成
--稍后,会调用以下代码:
function touchRandomImage()
index = 1到10之间的随机数(包含1和10)
moveImage = imageArray[index]
moveImage.touch = 您的TouchFunction
end
稍后,当其他代码调用image:touch()(或image.touch(image),同一个东西)时,只有根据以上方法随机选择的图像才会使用特殊的touch函数,所有其他图像都会进行无操作。
如果可以多次调用touchRandomImage(),则必须跟踪“先前”随机选择的图像,以便可以将其触摸字段重置为noTouch函数:
function touchRandomImage()
- prevIndex是一个全局变量或字段等
if prevIndex ~= nil then
imageArray[prevIndex].touch = noTouch
prevIndex = nil -- 以防万一
newIndex = 1到10之间的随机数(包含1和10)
moveImage = imageArray[newIndex]
moveImage.touch = 您的TouchFunction
prevIndex = newIndex
end
2013-11-20 23:15:35
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
你可能需要一个键值对列表来检索索引号,或者创建一个多维数组(一个用于保存索引,一个用于保存值)。
http://www.dotnetperls.com/keyvaluepair