如何在Lua中为随机生成的数字分配一个函数

首先我将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)
点赞
用户3009322
用户3009322

你可能需要一个键值对列表来检索索引号,或者创建一个多维数组(一个用于保存索引,一个用于保存值)。

http://www.dotnetperls.com/keyvaluepair

2013-11-19 14:49:21
用户869951
用户869951

抱歉我还是不清楚问题。但是在评论中无法解释清楚,所以尝试用这个答案。

以下是我认为您正在尝试进行的伪代码:

-- 初始化(启动)
function touch(image)
    …对图像做某些事情…
end

function noTouch(image)
    end -- 没有要触摸的内容

imageArray = 10张图像的数组
imageArray中的每个图像都是“Image”类的
对于imageArray中的每个图像,做如下处理:
image.touch = noTouch -- 一个“什么都不做”的函数
-- 初始化完成

--稍后,会调用以下代码:
function touchRandomImage()
    index = 110之间的随机数(包含110)
    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 = 110之间的随机数(包含110)
    moveImage = imageArray[newIndex]
    moveImage.touch = 您的TouchFunction
    prevIndex = newIndex
end
2013-11-20 23:15:35