views:

38

answers:

1

Hi, the following flash actionscript below stores letters and numbers into an array and displays them randomly in a flash movie as "falling text" down the screen. I'd like to modify this script so I can have the array store multiple images verses letters and numbers.

source: http://www.flashcomponents.net/tutorials/letter_and_number_rain_flash_tutorial/page_1.html

--- layer script 
    var letter_array = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0");

    var i = 0;

    setInterval(nLetter, 70);

    function nLetter()
    {
        duplicateMovieClip(letterMC, "letter" + i, 10 + i);

        i++


    }

    -------- symbol script

    onClipEvent(load)
    {

        var index = Math.round(Math.random() * _root.letter_array.length - 1)
        this.letter.text = _root.letter_array[index];
        _x = Math.round(Math.random() * 540) + 5
        _y = Math.round(Math.random() * -20) - 10;

        var gravity = 0.2;


        var ran = Math.round(Math.random() * 50) + 50;

        _alpha = ran + 10;
        _xscale = _yscale = ran;

        speed = Math.round(Math.random() * 5) + 10;

        rotSpeed = Math.round(Math.random() * 6) - 3;

        //this.letter.textColor = Math.random() * 0xFFFFFF


    }
    onClipEvent(enterFrame)
    {
        _y += speed;

        _rotation += rotSpeed

        speed += gravity;

        if(_y > 410)
        {
            this.removeMovieClip();
        }
    }
A: 

You need to add this at the top of your code:

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.*;

In the layer script you need to have an array of images, so the first thing would be to load all your images and store them in an array when the loading is complete.

in the following code , replace i "inferior to" totalNumImages with i < totalNumImages , I can't use the "<" character in the code.

//your images location
var url1:String = "images/image1.jpg";
var url2:String = "images/image2.jpg";

var urls:Array = [ url1 , url2 ];
var images:Array = [];

//the total number of images
var totalNumImages:int = 2;

for(var i:int ;i "inferior to" totalNumImages ; ++i )
{
   loadImage(images[i] );
}

function loadImage(url:String):void
{
  var loader:Loader = new Loader();
  var info:LoaderInfo = loader.contentLoaderInfo;

  info.addEventListener(Event.COMPLETE , loadComplete );
  info.addEventListener(IOErrorEvent.IO_ERROR , errorHandler );

  loader.load ( new URLRequest(url )); 
}

function errorHandler(event:IOErrorEvent):void
{
  trace( event );
}


function loadComplete(event:Event):void
{
    //assuming the order doesn't matter
    images.push(event.currentTarget.loader.content );

    //all images are loaded, let's start...
    if(images.length == totalNumImages )
       nLetter();
}

After all your images have loaded you can start the nLetter() function. Each time a MovieClip is added to the stage, it will load its image from the array of images that you've created.

change this:

this.letter.text = _root.letter_array[index];

to this:

//assumes that you have an array of DisplayObject
addChild( _root.images[index]);
PatrickS
thanks for your input. i'm still trying to get it working :/ i'm not sure where and what code i need to add to load images. sorry, i'm a newbie with flash as.
gates
check the edited answer
PatrickS
Is this what I should have the layer script? '//your images location var urls:Array = [url1, url2]; var images:Array = [usd.jpg, usd.jpg]; //the total number of images var totalNumImages:int = 2; for( var i:int; i) { function loadComplete(event:Event):void { //assuming the order doesn't matter images.push(event.currentTarget.content ); //all images are loaded, let's start... if(images.length == totalNumImages ) nLetter(); } }'
gates
how many images do you need to load?
PatrickS
there are 2 images
gates
PatrickS
it's still not running..changed image names to match your code. do I need to set "i" to equal 0? `var url1:String = "images/image1.jpg";var url2:String = "images/image2.jpg";var urls:Array = [ url1 , url2 ],var images:Array = [];var totalNumImages:int = 2;for( var i:int = 0 ; i ){ function loadComplete(event:Event):void{images.push(event.currentTarget.content ); if(images.length == totalNumImages ) nLetter();}function loadComplete(event:Event):void{images.push(event.currentTarget.content );if(images.length == totalNumImages )nLetter();}`
gates
check the edited code, didn't realize that one character couldn't be used in the code. if it still doesn't work , let me know if you have any error messages
PatrickS
I get "identifier expected" at the line: var images:Array = [];
gates
change this line to this:var urls:Array = [ url1 , url2 ]; i left a comma at the end, it should be a semi colon. sorry , edited too quickly!
PatrickS
is this script running for you? once i made that change, i got a bunch more errors. "the class or interface 'int' could not be loaded for totalNumImages, Loader, LoaderInfo, IOErrorEvent, and Event. Then there is a "type identifier is expected after ':' " error on the function loadImage.
gates
you're using AS2 and I'm on AS3. Can you possibly change to AS3? For the rest , Loader, LoaderInfo need to be imported, check the edited code. I'm not running this code, because it's a pretty standard script. Only we don't share the same environment and after all these messages I forgot you were on AS2!
PatrickS