Displaying a random image when an image is tapped?

I am a total noob to programming and lua ( i don’t have a developer program subscription anywhere yet! ) and am currently just developing apps for fun. I don’t know if this has been posted elsewhere, but I am having trouble making a random image appear when someone clicks on an image. I have 10 images in the folder. Here is my code so far.

[lua]local background = display.newImage ( “white.png” )
background.y = 500

local text = display.newText ( “Tap item to change.”, 5, 60, native.systemFont, 45 )
text:setTextColor ( 255, 0, 0 )
local r = math.random ( 1, 10 )
local item = display.newImage ( “[r].png”, 90, 350 )

function item:tap (event)
local r = math.random ( 1, 10 )
local item = display.newImage ("[r].png", 90, 350)
media.playEventSound ( “coin.wav” )
end
item:addEventListener ( “tap”, item ) [import]uid: 16721 topic_id: 11705 reply_id: 311705[/import]

I’m pretty sure this:

[lua]display.newImage ( “[r].png”, 90, 350 )[/lua]

…should look more like this:

[lua]display.newImage ( r … “.png”, 90, 350 )[/lua]

Assuming your images are named 1.png, 2.png, etc.

And just to make sure things are right, you could even do this:

[lua]display.newImage ( tostring® … “.png”, 90, 350 )[/lua]

You probably don’t need the tostring() function but it doesn’t hurt.

Jay
[import]uid: 9440 topic_id: 11705 reply_id: 42556[/import]

Thank you both on the speedy replies! What you guys suggested did work, however, now a new problem has popped up: the images don’t go away, they just stay there as new images pop up! Here is my changed code:

[lua]local background = display.newImage ( “white.png” )
background.y = 500

local text = display.newText ( “Tap item to change.”, 5, 60, native.systemFont, 45 )
text:setTextColor ( 255, 0, 0 )
local r = math.random ( 1, 10 )
local item = display.newImage ( r…".png", 90, 350 )
function item:tap (event)
local r = math.random ( 1, 10 )
local item = display.newImage (r…".png", 90, 350)
media.playEventSound ( “coin.wav” )
end
item:addEventListener ( “tap”, item )
[import]uid: 16721 topic_id: 11705 reply_id: 42559[/import]

The image won’t go unless you tell it to, change your code to this. Note the call to removeSelf to destroy the previous image on each tap.

  
local background = display.newImage ( "white.png" )  
background.y = 500  
   
local text = display.newText ( "Tap item to change.", 5, 60, native.systemFont, 45 )  
text:setTextColor ( 255, 0, 0 )  
  
local r = math.random ( 1, 10 )  
local item = display.newImage ( r..".png", 90, 350 )  
  
function item:tap (event)  
 local r = math.random ( 1, 10 )  
  
 if item and item["removeSelf"] then  
 item:removeSelf()  
 end  
  
 item = nil  
  
 item = display.newImage (r..".png", 90, 350)  
 media.playEventSound ( "coin.wav" )  
end  
  
item:addEventListener ( "tap", item )   
  

Hope it helps. [import]uid: 5833 topic_id: 11705 reply_id: 42560[/import]

And just a tip that may help you in the future…

…if you’re showing pictures of the numbers 1 through 10, then files names like 1.png, 2.png, etc., make sense. But if you’re showing cat.png, dog.png, and so on, there’s another way to handle that besides renaming your files.

Do this at the top of your code:

[lua]myPics = {“cat.png”, “dog.png”, “goat.png”, “cow.png”, “lizard.png”}[/lua]
Those are all the picture files you want to use.

Now when you want to grab a random picture, use code like this:

[lua]local r = math.random ( 1, #myPics )
local item = display.newImage ( myPics[r], 90, 350 )[/lua]

First thing to notice is the random function looks to see how many pictures you’re using instead of you “hard coding” the maximum to 10. That allows you to add/subtract pictures as you like, which is especially helpful while developing.

Second thing is that you’re then using that randomly generated number as an “index” into the myPics table to return the name of the file at that position, and then you’re loading that picture.

Just some techniques that might help make your development faster/easier/more fun. :wink:

Jay

[import]uid: 9440 topic_id: 11705 reply_id: 42564[/import]

A different approach we have taken is to define the table within a file called content.lua and include the file in the lua module where you need it.

content = require(“content”)

This file will simply contain a return followed by a table of rows with the data you want to access { id = 1, num = one. Pics = {cat, dog} } . you can associate pictures, sound file names etc as required.

that way your content remains external and easy to change without touching the code in addition to managing all via lua tables as mentioned above. [import]uid: 55009 topic_id: 11705 reply_id: 42584[/import]

Thanks again guys! but yet ANOTHER problem has popped up: the tapping only works once and then the image won’t do anything, not even coin.wav will play! Here again is my code: (does anyone know how to stop lua color coding when posting a comment on this forum?)

[lua]local background = display.newImage ( “white.png” )
background.y = 500

local text = display.newText ( “Tap item to change.”, 5, 60, native.systemFont, 45 )
text:setTextColor ( 255, 0, 0 )
local r = math.random ( 1, 10 )

local item = display.newImage ( r…".png", 90, 350 )

function item:tap (event)
local r = math.random ( 1, 10 )

if item and item[“removeSelf”] then
item:removeSelf ()
end

item = nil

local item = display.newImage (r…".png", 90, 350)
media.playEventSound ( “coin.wav” )
end

item:addEventListener ( “tap”, item ) [import]uid: 16721 topic_id: 11705 reply_id: 42637[/import]

Inside your tap event handler remove the “local” from this line:

local item = display.newImage (r..".png", 90, 350)  

Rather than redeclaring a new local variable as you are now you want to just assign the new image to the initial item variable declared on line 8. [import]uid: 5833 topic_id: 11705 reply_id: 42638[/import]

it still won’t repeat even after I applied your suggestions. Here is the code
[lua]local background = display.newImage ( “white.png” )
background.y = 500

local text = display.newText ( “Tap item to change.”, 5, 60, native.systemFont, 45 )
text:setTextColor ( 255, 0, 0 )
local r = math.random ( 1, 10 )

local item = display.newImage ( r…".png", 90, 350 )

function item:tap (event)
local r = math.random ( 1, 10 )

if item and item[“removeSelf”] then
item:removeSelf ()
end

item = nil

local item = display.newImage (r…".png", 90, 350)
media.playEventSound ( “coin.wav” )
end

item:addEventListener ( “tap”, item ) [import]uid: 16721 topic_id: 11705 reply_id: 42642[/import]

That code doesn’t have my change?

Are you getting any errors in the console? [import]uid: 5833 topic_id: 11705 reply_id: 42643[/import]

no i am not. [import]uid: 16721 topic_id: 11705 reply_id: 42644[/import]

oh, sorry about that. I actually did apply the change but i must not have pasted it into the browser window… [import]uid: 16721 topic_id: 11705 reply_id: 42645[/import]

never mind, guys. I made a solution myself: I made all of the images the same size so they overlap, making it appear as though they disappear. Thanks again! [import]uid: 16721 topic_id: 11705 reply_id: 42647[/import]

Just realised the problem, as you are adding the event listener to the image it gets removed when you destroy it.

Give this a try:

[code]

local background = display.newImage ( “white.png” )
background.y = 500

local text = display.newText ( “Tap item to change.”, 5, 60, native.systemFont, 45 )
text:setTextColor ( 255, 0, 0 )
local r = math.random ( 1, 10 )

local item = display.newImage ( r…".png", 90, 350 )

local function onTap( event )

local r = math.random ( 1, 10 )

if item and item[“removeSelf”] then
item:removeSelf()
end

item = nil

local item = display.newImage (r…".png", 90, 350)
media.playEventSound ( “coin.wav” )

item:addEventListener ( “tap”, onTap )
end

item:addEventListener ( “tap”, onTap )

[code] [import]uid: 5833 topic_id: 11705 reply_id: 42651[/import]

That did work, however now coin.wav keeps repeating itself. At first it plays only once, but as I keep tapping the amount of times it repeats increases. I copied your exact code, so I don’t know what the problem is. (I am using windows 7 Home Premium with corona build 2011.505 and LuaEdit version 3.0.3 build 196, if that helps at all.) [import]uid: 16721 topic_id: 11705 reply_id: 42656[/import]

I’ve just removed your dupe thread. In future edit your thread; do not create dupes.

I understand you are new, so please read the forum rules and guidelines; http://developer.anscamobile.com/forum/2011/05/05/forum-rules-and-guidelines

Thanks - and welcome :slight_smile: (Sorry, just have to keep a clean forum ;))

Peach [import]uid: 52491 topic_id: 11705 reply_id: 42663[/import]

Thanks to all of you, my program now runs very smoothly! [import]uid: 16721 topic_id: 11705 reply_id: 42760[/import]