A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: flying leaf, random,horizontal???

  1. #1
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197

    flying leaf, random,horizontal???

    Hello
    I have a countryside pic as part of the header with just the one tree in a corner from where I would like some leaves blowing away across the full width of the stage randomly. I could create lots of mc with one leaf each and stick them around the stage but it would look to repetitive to me.
    I am sure there must be a way to do it with actionscript, just need to know how.
    I posted this message in general help and got no answer. Looked for tutorial which i didn't find and downloaded fla which was like chinese to me. So I hope someone can explain it to me nice and simple.
    Using flash mx by the way...not 2004 version.
    thanks a lot
    Onsitus

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The following code is modified from my shooter game which has stars that move randomly horizontally.

    See the original to see the effect applied to stars.

    Jim's Shooter Game

    The basic idea is that you have a movieclip called 'leaves' in the library. Each frame of the movieclip contains an image (or animation) of a different leaf. You might want to animate the individual leaves to tumble.

    This script moves them across the screen at different rates.

    code:


    kNbrLeaves = 30;
    kLeafLayer = 100;

    SW = Stage.width; // keep track of width and height of stage
    SH = Stage.height;


    moveLeaf = function()
    {
    this._x -= this.speed;
    if (this._x < -10)
    this._x = SW+10;
    }



    // Initial Leaf setup
    for (i = 0; i < kNbrLeaves; ++i) {
    mc = _root.attachMovie('leaves', 'leaf_'+i, kLeafLayer+i);
    mc._x = random(SW);
    mc._y = random(SH);
    mc._xscale = mc._yscale = 50;
    mc._alpha = 50 + random(50);
    mc._rotation = random(360);
    mc.speed = random(10)+1;
    // select random leaf (by frame) mc.gotoAndStop(random(3)+1);
    mc.onEnterFrame = moveLeaf;
    }




    Something else you might want to try is varying the speed in the moveLeaf function. For example:

    code:

    moveLeaf = function()
    {
    // tweak the numbers .001 and 2 for different effects,
    // the first number affects rate of change,
    // and the second number affects amplitude of change
    this.speed += Math.sin(getTimer()*.001)*2;
    this._x -= this.speed;
    if (this._x < -10)
    this._x = SW+10;
    }


    Last edited by jbum; 10-13-2004 at 02:37 PM.

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    Thanks a lot for the answer and happy to know it can be done. Also I had a look at ur bestiary. Will try to understand the basic ones in my spare time.
    Still have a problem though...
    So I imported the pic of the leaf in the library and converted it into a movie clip called "leaves" made of 5 frames (just rotated the leaf a bit). Then placed the mc on the main stage on layer1 first frame, on layer2 first frame I placed the actionscript u gave me and...nothing. I tried to place the actionscript inside the mc "leaves", also on the same frame, gave the instance name of the mc "leaves"... Only see the one leaf moving on itself.
    Where did i go wrong?
    Also I do not want the leaves flying all around the stage but just in the top pic (if possible just in the sky). Shall I made a mc consisting of just the top pic and the leaves and load it into the main movie?
    I joined the fla to see what i mean.
    Thanks a lot again.
    Attached Files Attached Files

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    1)
    In order for attachmovie to work, you have to use the linkage dialog and export the movie. Ctrl-click (or right-click in Windows) the 'leaves' movie in the library and select Linkage... from the menu.

    Click the box that says "export for actionscript". This makes the name "leaves" available to the attachmovie command in actionscript.

    2) You left a leaf on the stage. Remove it - the script takes care of placing them on the stage for you.

    3) I changed your leaf setup as follows.
    I took out the scaling (used to make my stars tiny), and fixed the _y placement to put the leaves in the sky.

    code:

    kSkyBottom = 127;

    // Initial Leaf setup
    for (i = 0; i < kNbrLeaves; ++i) {
    mc = _root.attachMovie('leaves', 'leaf_'+i, kLeafLayer+i);
    mc._x = random(SW);
    mc._y = random(kSkyBottom);
    // mc._xscale = mc._yscale = 50;
    mc._alpha = 50 + random(50);
    mc._rotation = random(360);
    mc.speed = random(10)+1;
    // select random leaf (by frame)
    mc.gotoAndStop(random(5)+1);
    mc.onEnterFrame = moveLeaf;
    }



    4. If you want to move the leaves from left to right, use this version of the move function instead. The one you are using moves the leaves from right to left.

    code:

    // move leaves to the right
    moveLeaf = function()
    {
    this._x += this.speed;
    if (this._x > SW+10)
    this._x = -10;
    }



    5. If your individual frames are only rotating the leaves, you don't really need them, because the script already gives you random rotation. I would suggest using different leaf graphics for each frame.

  5. #5
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    Just perfect

  6. #6
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    one more problem...
    the pic at the back, when the leaves pass above the tree, it distorts itself (is that the right word!) Anything to do about it?

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I'm not seeing any distortion here... Enclose your latest version...

  8. #8
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Flash handles vector graphics better than raster ones. Draw the 'leaf' instead of using a picture.

    gparis

  9. #9
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    I'll try later on to draw leaf...lucky that it is small and noone can see it
    I'm joining the fla anyway.
    Thank you all for ur help, u've been great.
    Attached Files Attached Files

  10. #10
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    ok, did draw a leaf (called "leaf1") and i have the same result as with the pic leaf (called "leaf" in the library).
    Join u the file which is the same one as before but using "leaf1" instead of "leaf".
    Hoping u can help...

  11. #11
    Senior Member
    Join Date
    Nov 2002
    Location
    Nettuno
    Posts
    197
    ops forgot the file
    Attached Files Attached Files

  12. #12
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Hi,
    I saw no distortion, but i guess in your player it distorts because ther is movement over the image. It happens sometimes that raster images get 'shifted' or 'distorted'. One workaround is to embed the rater image into a graphic and set its _alpha to 99%. It's just one workaround.. also having all images symbols on round integers is always a good idea. yours is at 0,0 so that was not the problem.

    I attached the .fla . Tell me if better.

    gparis
    Attached Files Attached Files

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