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.,
Ex:
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)
Here is my code:
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)
You will probably need a keypair list to retrieve the index #, or make a multi dimension array (one to hold the index and one to hold the value.
http://www.dotnetperls.com/keyvaluepair
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:
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.
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: