Random display of images

Hi,

I need help with the code that makes a random selection of one image, in a group of many, to appear at a time when I chance the scene. What should I use?

Thanks

Gustavo [import]uid: 95495 topic_id: 16653 reply_id: 316653[/import]

I’d use an array and randomly pick a image…

I’m pretty sure this will work… if it doesn’t just tell me…!
got to help Mom with dinner! :stuck_out_tongue:

[code]
array= {}

image1= (“image1.png”)
image2= (“image2.png”)
image3= (“image3.png”)

table.insert(array,image1)
table.insert(array,image2)
table.insert(array,image3)
– and so on…

function selectimage()
display.newImage(array,math.random(1,3))
end

selectimage() [import]uid: 95032 topic_id: 16653 reply_id: 62200[/import]

It didn’t work!

Director ERROR: Failed to execute new( params ) function on ‘final’.

bad argument #-1 to ‘_newImage’ (Proxy expected, got nil)

Now what?
Should I insert those images in my local group (to return to main)? [import]uid: 95495 topic_id: 16653 reply_id: 62212[/import]

I’ll get t working if I have some time…! [import]uid: 95032 topic_id: 16653 reply_id: 62215[/import]

Thanks for your help!!! [import]uid: 95495 topic_id: 16653 reply_id: 62220[/import]

Acctually I can’t find out what to do… I’m so sorry, wish I could help… someone else will know I’m sure :slight_smile: [import]uid: 95032 topic_id: 16653 reply_id: 62378[/import]

How about like this…

[code]array= {}
local i = 1
array[i]= display.newImage(“image1.png”);array[i].alpha=0;i=i+1
array[i]= display.newImage(“image2.png”);array[i].alpha=0;i=i+1
array[i]= display.newImage(“image3.png”);array[i].alpha=0;i=i+1
–you can copy and paste the above lines and just change the filename

function selectimage()
local randomimage = math.random(1,#array)
–1 to highest index in the table
array[randomimage].alpha=1
end

selectimage()
[/code]

I would do it something like that. (I have not tested this but it should work) [import]uid: 40100 topic_id: 16653 reply_id: 62396[/import]

Great!! It worked!!

One more question though: now the images appear in the topleft corner…how do I position them? They will all be at the center…do I have to do one at a time or position the array itself?

Thanks a lot!!! [import]uid: 95495 topic_id: 16653 reply_id: 62474[/import]

array[i].x=200 – position x
array[i].y=200 – position y [import]uid: 86417 topic_id: 16653 reply_id: 62490[/import]

I tried something like that, but i got

Director ERROR: Failed to execute new( params ) function on ‘final’.

attempt to index field ‘?’ (a nil value)

on that line!!
[import]uid: 95495 topic_id: 16653 reply_id: 62492[/import]

array= {}
local i = 1

array[i]= display.newImage(“image1.png”);array[i].alpha=0;i=i+1

array[i]= display.newImage(“image2.png”);array[i].alpha=0;i=i+1

array[3]= display.newImage(“image3.png”);array[i].alpha=0;i=i+1
–you can copy and paste the above lines and just change the filename

function selectimage()
local randomimage = math.random(1,#array)
–1 to highest index in the table
array[randomimage].alpha=1
array[randomimage].x=400
array[randomimage].y=200
end

selectimage()

working!!! [import]uid: 86417 topic_id: 16653 reply_id: 62499[/import]

duuude you guys are awesome…thanks a lot!!!

Last question, I swear…lol

How do I set the size of the images now? I need their width to be padronized as the content.Width!! [import]uid: 95495 topic_id: 16653 reply_id: 62508[/import]

array= {}
local i = 1

array[i]= display.newImage(“image1.png”);array[i].alpha=0;i=i+1

array[i]= display.newImage(“image2.png”);array[i].alpha=0;i=i+1

array[3]= display.newImage(“image3.png”);array[i].alpha=0;i=i+1
–you can copy and paste the above lines and just change the filename

function selectimage()
local randomimage = math.random(1,#array)
–1 to highest index in the table
array[randomimage].alpha=1
array[randomimage].x=400
array[randomimage].y=200
array[randomimage].xScale=0.3
array[randomimage].yScale=0.3
end

selectimage()

There;)
[import]uid: 86417 topic_id: 16653 reply_id: 62525[/import]

I would recommend sizing and positoning the images when you create the array (the less you have to do to the image later the better, do as much processing as needed when you set stuff up, ok so your app might take an extra second to open but it will preform better after it is… may make no differance on your project if it is small)

array[i]= display.newImage(“image1.png”);
array[i].x=400;
array[i].y=200;
array[i].alpha=0;
array[i].xScale=0.3;
array[i].yScale=0.3;
i=i+1;

That only leaves setting the alpha in your function. (might want to use display.newImageRect)

(oh… the “;” is the same as a new line… sometimes it makes the code, or blocks of code, easier to handle)

array[i]= display.newImage(“image1.png”);array[i].x=400;array[i].y=200;array[i].alpha=0;array[i].xScale=0.3;array[i].yScale=0.3;i=i+1; [import]uid: 40100 topic_id: 16653 reply_id: 62536[/import]