To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-13-2004, 02:48 PM   #1
TeachMe
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
TeachMe is offline   Reply With Quote
Old 10-13-2004, 03:34 PM   #2
jbum
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 03:37 PM.
jbum is offline   Reply With Quote
Old 10-13-2004, 05:56 PM   #3
TeachMe
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
File Type: fla leaf.fla (112.0 KB, 1042 views)
TeachMe is offline   Reply With Quote
Old 10-13-2004, 06:13 PM   #4
jbum
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.
__________________
jbum is offline   Reply With Quote
Old 10-13-2004, 06:58 PM   #5
TeachMe
Senior Member
 
Join Date: Nov 2002
Location: Nettuno
Posts: 197
Just perfect
TeachMe is offline   Reply With Quote
Old 10-13-2004, 07:23 PM   #6
TeachMe
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?
TeachMe is offline   Reply With Quote
Old 10-13-2004, 07:53 PM   #7
jbum
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...
__________________
jbum is offline   Reply With Quote
Old 10-13-2004, 07:54 PM   #8
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,239
Flash handles vector graphics better than raster ones. Draw the 'leaf' instead of using a picture.

gparis
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 10-14-2004, 03:17 AM   #9
TeachMe
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
File Type: fla logoanne.fla (40.0 KB, 576 views)
TeachMe is offline   Reply With Quote
Old 10-14-2004, 06:04 AM   #10
TeachMe
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...
TeachMe is offline   Reply With Quote
Old 10-14-2004, 06:05 AM   #11
TeachMe
Senior Member
 
Join Date: Nov 2002
Location: Nettuno
Posts: 197
ops forgot the file
Attached Files
File Type: fla logoanne.fla (48.0 KB, 407 views)
TeachMe is offline   Reply With Quote
Old 10-14-2004, 01:03 PM   #12
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,239
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
File Type: zip logoanne.fla.zip (17.6 KB, 875 views)
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:20 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.