如何在Lua中为随机生成的数字分配功能 [英] How to assign a function in Lua for a randomly generated numbers
问题描述
首先,我将1-10个数组中的10张图像作为键值存储在数组(即表格)中,然后使用0-9之间的math.random
函数创建一个随机数.
并且我需要通过random函数创建的值访问存储在数组中的图像,并为特定的图像文件单独分配触摸功能.
First I stored 10 images in array(i.e., table) from 1-10 as key values and I create a random number using math.random
function between 0-9.,
and I need to access the image that is stored in array by the value created by random function, and assign the touch function for the particular image file alone.,
例如:
如果随机函数将数字创建为"5",则需要将存储在数组索引中的图像5.png移为5.除5.png以外的其他图像均不应使用触摸功能.(即,它们是不允许在屏幕上移动,但需要在屏幕上显示)
If the random function creates number as "5" I need to move the image 5.png that is stored in array index as 5 .,other images except 5.png should not use touch function., (i.e., they are not allowed to move in screen but need to display in screen)
这是我的代码:
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)
推荐答案
对不起,我仍然不清楚这个问题.但是注释中不清楚,因此请尝试此答案.
Sorry I am still not clear about the problem. But can't be clear in comments, so trying this answer.
这是我想您要尝试使用的伪代码:
Here is what I think you are trying to do, in pseudocode:
-- 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.
Later when some other code calls image:touch() (or image.touch(image), same thing), only the randomly selected image as per above will use the special touch function, all the others will have the do-nothing noTouch.
如果可以多次调用touchRandomImage(),则必须跟踪随机选择的上一个"图像,以便将其触摸字段重置为noTouch函数:
If touchRandomImage() can be called multiple times, you have to keep track of the "previous" randomly selected image so that you can reset its touch field to be the noTouch function:
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
这篇关于如何在Lua中为随机生成的数字分配功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!