Link to home
Start Free TrialLog in
Avatar of cthomasmn
cthomasmn

asked on

Dynamic image loading on button click

I am looking for code in Actionscript 3.0 which will allow a user to dynamically load a different image each time a single button is clicked.
Avatar of rascalpants
rascalpants
Flag of United States of America image

are you looking for a Class or just framescript?

rp
Also, is this for use in Flex or Flash?

and what is your data source you plan on using to hold the values of the image file names? (Web Services, XML, Remoting, Text file, etc)

rp
Avatar of cthomasmn
cthomasmn

ASKER

rp.

I am looking for framescript and I am using Flash CS3

Chuck
what is your data source?

rp
if you are publishing for Flash Player 9, then you should use the Loader Class...

if you are publishing for version 8, you should use the MovieClipLoader class.

but basically, you start off with an array of your data...

var imgList:Array = new Array();
imgList.push("image1.jpg");
imgList.push("image2.jpg");
imgList.push("image3.jpg");
imgList.push("image4.jpg");
imgList.push("image5.jpg");

then you use the Loader class to pull in the image you want...

but first you need to select a random number from that array...

so you can use this...

var selectedImg:String = imgList( (Math.round(Math.random*5)) - 1 );

(you might check the syntax on that one)

so you have a reference to a random image, and not you just need to use the loader class to pull in that image...

here is a link about the Loader class:

http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00003207.html

and here is another one...

http://newmovieclip.wordpress.com/2007/03/14/preloader-in-flash-cs3-actionscript-30-way/


rp


rp,

Thanks for your help and patience. I a simple AS3 file with a button. I want the images to load one at a time , each time the button is pressed. How do you make the button load the images from the Array?

Thanks
Chuck
rp,

Thanks for your help and patience. I a simple AS3 file with a button. I want the images to load one at a time , each time the button is pressed. How do you make the button load the images from the Array?

Thanks
Chuck

something like this maybe:

function selectImage()
{
      var rn:Number = Math.floor(Math.random()* imgList.length );
      var selectedImg:String = imgList[ rn ];
      // put code here to load the selected image...
}

myButton.addEventListener(MouseEvent.MOUSE_DOWN, selectImage);


ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rp,

Thanks. It worked perfectly.

Chuck