A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Quasi-newbie AS2.0 unique ransom image help

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Brooklyn
    Posts
    4

    Quasi-newbie AS2.0 unique ransom image help

    hey all,

    I'll admit up front I am a a sort of ActionScript neophyte. I have a client web site I built in AS2.0, and have been reluctant to overhaul in AS3.0. In any event, the client is asking me to add a front door slide show that cycles randomly through 11 product images. Seems easy enough, and I have managed to do that, but the problem I am having is how to get 11 unique numbers (i.e., image "no.2," for example, gets chosen twice in a row, and I want to avoid that).

    I know the solution has to do with arrays, which I am not entirely sure I understand. I get the basic concept (i.e., Flash keeping tracking of which 11 random numbers have already been used when determining the next random number), but I don't totally understand how to insert that into the code/which frame in the timeline.

    My .fla is available here: www.nkhstudio.com/teaserv2.fla.zip. Made in CS4.

    It's very simple, basically 12 frames; 1st frame is the AS that generates a random number 2-12, then goes to and plays frames 2 through 12 depending on the number generated. Problem I have is how to get Flash to remember the number chosen, so that when it returns to frame 1 to generate a new random number, it doesn't pick from what it has previously chosen.

    I have scoured this forum and others and while there are lots of posts about arrays and generating random numbers, the link I am missing is how particularly to apply this to Flash going to a particular frame to display an image/MC. Perhaps my whole structure (12 frames, AS on frame 1, images on frames 2-12) is not the best. I don't know. Any help is appreciated!

    many thanks,

    -Nate

  2. #2
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    the way yo doing it by frame.. doesnt really make sense to me.

    once you leave a frame that has code/actions in it..

    that code is lost.

    this is why people NEST movieClip and conent.. and put code or data in a timeline ABOVE it..

    so so matte what frame or state of the nested/child clip.. it can always access the timeline above it for code/variables/data...etc


    so the way your approaching is not going to work really.


    nest your 'product frames' into their own movieClip..

    and put this code in the main timeline..

    actionscript Code:
    var origArray:Array = new Array();
    //populate array with frame number choices
    for(i=2; i<13; i++){
        origArray.push(i);
    }
    //check to see if populated correctly
    trace("TOTAL LENGTH: "+origArray.length);
    //duplicate origArray
    var tempArray:Array = origArray.slice();

    var time = setInterval(pickRandom, 100);

    function pickRandom(){
        if(tempArray.length == 0){
            clearInterval(time);
            trace("---DONE---");
        }else{
            var ranNum:Number = random(tempArray.length);
            trace("RANDOM CHOICE: "+tempArray[ranNum]);
                    targetProductClip.gotoAndPlay(tempArray[ranNum]);
            tempArray.splice(ranNum, 1);                
        }
    }

    Code summary:

    starts by create a new (empty) array (origArray)

    I use a loop to populate that array with the frame numbers you mentioned (2-12, although after you nest things..this may change)

    I duplicate/copy this array (so I dont alter the original during the process), called (tempArray)

    I create a setInveral() timer..so I can test the function call..over and over..

    ensuring that my function is working properly..

    which is as follows:

    1.) check to see if the tempAray length is 0 (empty)..if so..stop.
    if not = 0... the generate a random number based on the length of the array left.

    2.) then I move my targetClip (this would be your movieClip with the 12 frames in it, each with a product image in it?) to the frame generated by the tempArray & random number (tempArray[ranNum])

    3.) I use this same randomly generated number to SPLICE that index/value from the array (so it cant be used again)

    then I start all over.. (however the temp array is now different/shorter..and all previously chosen index's have been removed)

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Location
    Brooklyn
    Posts
    4

    response to whisper

    Hi Whispers,

    Thanks for the reply, although I still don't seem to be doing something correct using your method. Here is how I followed your instructions:

    1. nested all 11 of my images into a MC (I put stop actions on each frame)
    2. placed that MC into my main timeline on frame 1, and gave it the instance name "Product"
    3. also on frame 1 (in a different "actions" layer), I added the code you provided.

    So my .fla file, at the main timeline, only has 1 frame, which has your code and my MC. Seems quite simple.

    The problem is that when I run the file, I see my output window generating random numbers as it's supposed to (so this runs perfectly), but it doesn't then trigger the corresponding frame number in my MC. In fact it always plays frame 1 of the MC and that's it. My suspicion is something is being lost in the translation, i.e., translating the random number into a value that the MC's timeline can use.

    Also, I noticed in the output window that once all random numbers, 1-11 are generated, the computation stops (it doesn't then start all over again and regenerate a new set of number based 1-11, and on and on...).

    My file is here if you want to see it:

    www.nkhstudio.com/teaser2.fla.zip

    Thanks again for your help!

    -Nate

  4. #4
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    1. needs to be in CS3 compatible format for me to open up..

    2.) in the code.. did you change the

    actionscript Code:
    var origArray:Array = new Array();
    //populate array with frame number choices
    for(i=2; i<13; i++){
        origArray.push(i);
    }
    //check to see if populated correctly
    trace("TOTAL LENGTH: "+origArray.length);
    //duplicate origArray
    var tempArray:Array = origArray.slice();

    var time = setInterval(pickRandom, 100);

    function pickRandom(){
        if(tempArray.length == 0){
            clearInterval(time);
            trace("---DONE---");
        }else{
            var ranNum:Number = random(tempArray.length);
            trace("RANDOM CHOICE: "+tempArray[ranNum]);
            targetProductClip.gotoAndPlay(tempArray[ranNum]);
            tempArray.splice(ranNum, 1);              
        }
    }

    this line:
    targetProductClip

    to be your targetClip instance name?

    (I think you stated it was Product)

    as for your other question.. the FUNCTION was the problem/solution/..

    I just put a timer on it:

    var time = setInterval(pickRandom, 100);

    (goes REALY REALY fast.. 10 times a second)..

    the timer keeps going for 11 times then stops..

    this can be triggered and stopped at any time though..

    it will take only MINOR tweaks once you get to the point above..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center