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

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! :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()

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)?

I'll get t working if I have some time.....!

Thanks for your help!!!

Acctually I can't find out what to do... I'm so sorry, wish I could help.. someone else will know I'm sure :)

How about like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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() 

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!!!

array[i].x=200 -- position x
array[i].y=200 -- position y

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!!

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!!!

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!!

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;)

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;

views:1534 update:2011/10/22 9:46:13
corona forums © 2003-2011