开发者

set array to hold images verses text, flash actionscript

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜