Pass the value of an array into a function

[edit: added code tag for clarity in reading, and images array added]
I am trying to pass the value of an array (to see if it is the correct answer) into a function that will evaluate if it isCorrect()

[code]–build array for the level
local quadlevels = {
{ name=“level01”, hint=“Hint For Level 01”, correctAns=“01_Sky_A.jpg”, quad1=“01_Sky_A.jpg”, quad2=“01_Sky_B.jpg”, quad3=“01_Sky_C.jpg”, quad4=“01_Sky_D.jpg”, },

{ name=“level02”, hint=“Hint for Level 02”, correctAns=“02_Water_B.jpg”, quad1=“02_Water_A.jpg”, quad2=“02_Water_B.jpg”, quad3=“02_Water_C.jpg”, quad4=“02_Water_D.jpg”, }
}

local images = {quadlevels[gameLevelValue].quad1, quadlevels[gameLevelValue].quad2, quadlevels[gameLevelValue].quad3, quadlevels[gameLevelValue].quad4}

index = 1
for i = 1, 2 do
for j = 1 , 2 do
local imageLoop = {}
imageLoop[index] = display.newImageRect(group, images[index], 300, 300)
transition.from(imageLoop[index], {time=2000, alpha=0})
group:insert(imageLoop[index])
imageLoop[index]:setReferencePoint(display.TopLeftReferencePoint)
imageLoop[index]:addEventListener(‘touch’,isCorrect) --imageLoop[index]:addEventListener(‘touch’,isCorrect(“clickedItemName”))
index = index + 1
end
end

function isCorrect(clickedItemName)
if clickedItemName = correctAns.Value, “Good Job”
else
if clickedItemName <>correctAns.Value, “Incorrect”
end
[/code]

Easy right? I just don’t know the syntax.

Does anyone know how to pass a value into a function?
–Shawn
[import]uid: 132937 topic_id: 35096 reply_id: 335096[/import]

When you create an event listener, you do not pass it parameters. It gets a single parameter automatically passed to it called “event” (well you can name it what you want). The event parameter for touch events includes a member called target (event.target) which is the object that was touched.

It’s hard to make specific recommendations for a couple of reasons. First it’s really helpful when you post code to wrap the code in <code> and </code> tags so it will retain your formatting and provide some syntax highlighting.

Secondly, you’re loops are building a set of tiles from an array called images which isn’t listed. Also you never use quadlevels anywhere. But in general terms, I’m guessing you want something like:

local imageLoop = {} -- moved here because in your code, it only lives inside the 2nd for looop  
index = 1  
for i = 1, 2 do  
 for j = 1 , 2 do  
 imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  
 transition.from(imageLoop[index], {time=2000, alpha=0}) -- do you want this to disappear after 2 second?  
 group:insert(imageLoop[index])  
  
 local function isCorrect(event)  
 if event.phase == "ended" then  
 if event.target == correctAns.Value then  
 -- show the user "Good Job", might want a display.newText() for that  
 else  
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 end  
  
 imageLoop[index]:setReferencePoint(display.TopLeftReferencePoint)  
 imageLoop[index]:addEventListener('touch',isCorrect) index = index + 1  
 end  
end  
  

Now that code’s probably not going to work exactly like you want it because you’re not using your arrays above and I’m not sure how you intend to do it, but you should be able to study the difference in how to setup a touch handler function, how to construct and if-then-else test and get you on the right path.
[import]uid: 199310 topic_id: 35096 reply_id: 139569[/import]

Note: I modified the original post to include code tags for readability.

This is perfect as an example. Thank you!!

You put me on the path to this article.

http://www.coronalabs.com/blog/2012/04/11/corona-touch-events-tutorial/

Between the code above and the article, I should be able to get this to work. [import]uid: 132937 topic_id: 35096 reply_id: 139573[/import]

Here is where I am stuck.

event.target always equals some random hex string from a table (which I cannot predict to evaluate)

function isCorrect(event)  
  
 if event.phase == "ended" then  
 if event.target == quadlevels[gameLevelValue].correct  
then   
 print("you win")  
 -- show the user "Good Job", might want a display.newText() for that  
 else   
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 --end  
end  

That is why I was trying to pass a value to the function. If I could pass the image name to the function and evaluate if it == quadlevels[gameLevelValue].correct, then I would know they clicked the right image, otherwise, they need to keep playing. [import]uid: 132937 topic_id: 35096 reply_id: 139608[/import]

event.target is the display object itself. In other words:

imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  

It’s the display.newImageRect() that’s assigned to imageLoop[some index value]. So what I would do as part of creating your images is something like:

for i = 1, 2 do  
 for j = 1 , 2 do  
 local imageLoop = {}   
 imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  
 imageLoop[index].imageName = images[index] -- since you're comparing the correct answer to the filename  
 end  
end  

then make your event handler more like:

function isCorrect(event)  
   
 if event.phase == "ended" then  
 if event.target.imageName == quadlevels[gameLevelValue].correct then   
 print("you win")  
 -- show the user "Good Job", might want a display.newText() for that  
 else   
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 --end  
end  

Or something like that.
[import]uid: 199310 topic_id: 35096 reply_id: 139660[/import]

Yep, that is part I was missing. It worked perfectly once I added

 imageLoop[index].imageName = images[index] 

to the loop. The imageName is passed to the function and can be evaluated.

This is exactly what I needed. Thank-you. [import]uid: 132937 topic_id: 35096 reply_id: 139661[/import]

When you create an event listener, you do not pass it parameters. It gets a single parameter automatically passed to it called “event” (well you can name it what you want). The event parameter for touch events includes a member called target (event.target) which is the object that was touched.

It’s hard to make specific recommendations for a couple of reasons. First it’s really helpful when you post code to wrap the code in <code> and </code> tags so it will retain your formatting and provide some syntax highlighting.

Secondly, you’re loops are building a set of tiles from an array called images which isn’t listed. Also you never use quadlevels anywhere. But in general terms, I’m guessing you want something like:

local imageLoop = {} -- moved here because in your code, it only lives inside the 2nd for looop  
index = 1  
for i = 1, 2 do  
 for j = 1 , 2 do  
 imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  
 transition.from(imageLoop[index], {time=2000, alpha=0}) -- do you want this to disappear after 2 second?  
 group:insert(imageLoop[index])  
  
 local function isCorrect(event)  
 if event.phase == "ended" then  
 if event.target == correctAns.Value then  
 -- show the user "Good Job", might want a display.newText() for that  
 else  
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 end  
  
 imageLoop[index]:setReferencePoint(display.TopLeftReferencePoint)  
 imageLoop[index]:addEventListener('touch',isCorrect) index = index + 1  
 end  
end  
  

Now that code’s probably not going to work exactly like you want it because you’re not using your arrays above and I’m not sure how you intend to do it, but you should be able to study the difference in how to setup a touch handler function, how to construct and if-then-else test and get you on the right path.
[import]uid: 199310 topic_id: 35096 reply_id: 139569[/import]

Note: I modified the original post to include code tags for readability.

This is perfect as an example. Thank you!!

You put me on the path to this article.

http://www.coronalabs.com/blog/2012/04/11/corona-touch-events-tutorial/

Between the code above and the article, I should be able to get this to work. [import]uid: 132937 topic_id: 35096 reply_id: 139573[/import]

Here is where I am stuck.

event.target always equals some random hex string from a table (which I cannot predict to evaluate)

function isCorrect(event)  
  
 if event.phase == "ended" then  
 if event.target == quadlevels[gameLevelValue].correct  
then   
 print("you win")  
 -- show the user "Good Job", might want a display.newText() for that  
 else   
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 --end  
end  

That is why I was trying to pass a value to the function. If I could pass the image name to the function and evaluate if it == quadlevels[gameLevelValue].correct, then I would know they clicked the right image, otherwise, they need to keep playing. [import]uid: 132937 topic_id: 35096 reply_id: 139608[/import]

event.target is the display object itself. In other words:

imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  

It’s the display.newImageRect() that’s assigned to imageLoop[some index value]. So what I would do as part of creating your images is something like:

for i = 1, 2 do  
 for j = 1 , 2 do  
 local imageLoop = {}   
 imageLoop[index] = display.newImageRect(group, images[index], 300, 300)  
 imageLoop[index].imageName = images[index] -- since you're comparing the correct answer to the filename  
 end  
end  

then make your event handler more like:

function isCorrect(event)  
   
 if event.phase == "ended" then  
 if event.target.imageName == quadlevels[gameLevelValue].correct then   
 print("you win")  
 -- show the user "Good Job", might want a display.newText() for that  
 else   
 -- show the user "Incorrect"  
 end  
 end  
 return true -- very important to do this  
 --end  
end  

Or something like that.
[import]uid: 199310 topic_id: 35096 reply_id: 139660[/import]

Yep, that is part I was missing. It worked perfectly once I added

 imageLoop[index].imageName = images[index] 

to the loop. The imageName is passed to the function and can be evaluated.

This is exactly what I needed. Thank-you. [import]uid: 132937 topic_id: 35096 reply_id: 139661[/import]