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

时间:2013-11-19 14:36:09

标签: function random lua corona

首先,我将1-10个数组(即表格)中的10个图像存储为关键值,然后使用0-9之间的math.random函数创建一个随机数。 我需要通过随机函数创建的值访问存储在数组中的图像,并单独为特定图像文件分配触摸功能。

例如:

如果随机函数将数字创建为“5”,我需要将存储在数组索引中的图像5.png移动为5。除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)
--assigning values
    dig1 = myres1
    dig2 = myres2

 function dig1:touch(event)
    local t = event.target
    -- printTouch(event)
    local phase = event.phase
        if phase == "began" then
            -- Make target the top-most object
                local parent = t.parent
                parent:insert(t)
                display.getCurrentStage():setFocus(t)
    -- This flag is to prevent spurious events being sent to the target
                t.isFocus = true
    -- Store initial position
                t.x0 = event.x - t.x
                t.y0 = event.y - t.y
    -- Make myObject temporarily kinematic
                event.target.bodyType = "kinematic"
    -- Stop current motion, if any
                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
                -- Move piece to target
                    transition.to(t,{
                        time = 150,
                        x = currentTarget.x,
                        y = currentTarget.y
                    })
                    currentTarget = nil
                    isHighlighted = false
                end
            display.getCurrentStage():setFocus(nil)
            t.isFocus = false
            -- Switch body type back to "static"
            event.target.bodyType = "static"
        end
    end

    return true
end

dig1:addEventListener("touch",dig1)

2 个答案:

答案 0 :(得分:1)

抱歉,我仍然不清楚这个问题。但是在评论中无法明确,所以尝试这个答案。

以下是我认为您尝试做的事情,伪代码:

-- init (startup)
function touch(image) 
    ... do something to image ...
end

function noTouch(image) 
    end -- nothing to touch

imageArray = table of 10 images
each image in imageArray is of "class" Image
for each image in imageArray do
    image.touch = noTouch -- a "do nothing" function
end
-- init completed

-- later, this gets called:
function touchRandomImage()
    index = random number between 1 and 10 (incl)
    moveImage = imageArray[index]
    moveImage.touch = yourTouchFunction
end

稍后当其他一些代码调用image:touch()(或image.touch(image),同样的东西)时,只有上面随机选择的图像才会使用特殊的触摸功能,所有其他代码都会使用没有noTouch。

如果可以多次调用touchRandomImage(),则必须跟踪“之前”随机选择的图像,以便您可以将其触摸字段重置为noTouch函数:

function touchRandomImage()
    -- prevIndex is a global, or a field etc
    if prevIndex ~= nil then
        imageArray[prevIndex].touch = noTouch
        prevIndex = nil -- in case exception etc
    newIndex = random number between 1 and 10 (incl)
    moveImage = imageArray[newIndex]
    moveImage.touch = yourTouchFunction
    prevIndex = newIndex
end

答案 1 :(得分:0)

您可能需要一个密钥对列表来检索索引#,或者创建一个多维数组(一个用于保存索引,另一个用于保存值。

http://www.dotnetperls.com/keyvaluepair