May 21, 2012, 11:00:40 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Prowlie Welcome back! The site migration is complete and normal service resumed.
Advanced search
Pages: [1]   Go Down
Print
Author Topic: Actionscript help needed! (now with .fla example)  (Read 1575 times)
0 Members and 1 Guest are viewing this topic.
Esn
MotM
Heroic
**
Offline Offline

Gender: Male
Posts: 2096


My snake oil is of the highest quality, guaranteed


WWW
« on: December 20, 2008, 10:40:52 AM »

Ok... here's an fla:
http://www.mediafire.com/?xmm5jzyb3qm

Its structure is as follows:

On the main timeline:

-2 layers, a layer for the black background and a layer for the snow
-300 frames
-keyframes on snow layer on frames 1, 101 and 201


The following actions are added for the snow_mc movieclip on frame 1:
Code:
snowflakes = 100;
do {
   duplicateMovieClip(snow, "snow"+k, k);
   k++;
} while (k != snowflakes);
On frames 101 and 201, the # of snowflakes changes to 101 and 102.

The following code is attached to frame 1 on the snow layer:
Code:
this.onEnterFrame = function() {
if (!(this._currentframe >= 1 && this._currentframe<=100)){
 snow_mc.gotoAndStop(1);
} else if (!(this._currentframe >= 101 && this._currentframe<=200)){
 snow_mc.gotoAndStop(2);
} else {
 snow_mc.gotoAndStop(3);
}
}
Inside the snow_mc movieclip, there is another movieclip named "snow", inside of which is the small .png image which is the snowflake.

There are 3 keyframes inside the snow_mc movieclip. To the "snow" movieclip on the 1st keyframe, the following code is attached:
Code:
onClipEvent (load) {
    //variables
    width = 600;
    height = 429;
    //random x,y, and alpha
    this._xscale = this._yscale=50+Math.random()*100;
    this._alpha = 20+Math.random()*50;
    //random x and y for flakes
    this._x = -width+Math.random()*(3*width);
    this._y = -10+Math.random()*height;
    //speed and trigonometric value
    i = 1+Math.random()*2;
    k = -Math.PI+Math.random()*Math.PI;
    rad = 0;
}
onClipEvent (enterFrame) {
    // horizontal movement
    rad += (k/180)*Math.PI;
    xmovement = width * 0.5;
    this._x -= Math.cos(rad)+(xmovement-(width/2))/50;
    // vertical movement
    this._y += i;
    // remove clips when they misbehave (overstep boundaries)
    if (this._x>(width+50)) {
        this._x = -45;
        this._y = Math.random()*height*2;
    }
    if (this._x<-50) {
        this._x = width+45;
        this._y = Math.random()*height*2;
    }
    if (this._y>=height) {
        this._y = -50;
        this._x = -width+Math.random()*(3*width);
    }
}
On keyframes 2 and 3, it's the same thing except on frame 2:
Code:
xmovement = width * 1.5;
and on frame 3:
Code:
xmovement = -width * 1.5;
This value controls the direction of the wind. "width * 0.5" is 0, so it should be obvious when the movieclip moves to frames 2 or 3.

When I try to make a .swf from this, Flash MX gives me the following error message:
Code:
Scene=Scene 1, Layer=Layer 2, Frame=201: Line 2: Statement must appear within on/onClipEvent handler
     do {

P.S. I got a PM from glosrfc on another forum, part of which I will repost here with some comments:

Quote
I did say that you need to check the currentframe inside an onEnterFrame handler. The syntax on the main timeline is:
this.onEnterFrame = function() {
// code to check _currentframe here
}
Ok... I hope I did that right in the code I just posted. Though it seems to be giving me errors.

Quote
One of the problems you seem to have is controlling all of the different timelines that are running. For example, if you have a snow mc referenced with this...what does this refer to when you swap the first snow mc for a second snow mc? That's one of the reasons why it's recommended that all code is placed on the main timeline rather than placed directly onto movieclips.
So if "this" is a problem (though it seemed to work when there were just two keyframes in the movieclip), what's the alternative?

Quote
Finally, did you have any joy with the idea about passing your variables into the movieclips, instead of hardcoding them into the movieclips themselves? There are a few reasons why this approach is preferable:
1. You can control the variables from the main timeline without having to check which frame the playhead is currently on.
2. You don't have to edit (and then remember) different settings inside different MCs
3. You can use multiple instances of a single MC rather than create new MCs for each different variable..this will reduce the overall size of the final SWF.
4. Using instance names makes it simpler to target your symbols.
Well, it seems that I can already change the # of snowflakes by making new keyframes on the snow layer in the main timeline. What I really need to be able to do is to change the windspeed (which is controlled by that "xmovement" parameter in the movieclip). Is it possible to move that parameter into the main timeline, and if so, could someone make an example .fla?
Logged

<a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=november%2006.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=november%2006.swf</a>
My blog about Russian animation
My Newgrounds account (animation, art, music)
My DeviantArt account (comics, photos, writing)
Brass ensemble recordings (in which I play tuba & euphonium)
Esn
MotM
Heroic
**
Offline Offline

Gender: Male
Posts: 2096


My snake oil is of the highest quality, guaranteed


WWW
« Reply #1 on: December 21, 2008, 04:43:00 AM »

Vector helped me with one problem (via PM); I removed the "!" signs because they reverse what's in the brackets (I did not know this).

However, I still have another problem:

When I check the code applied to the snow_mc movieclip in the main timeline for errors, I get the following error message:

Quote
Scene=Scene 1, Layer=Layer 70, Frame=2: Line 2: Statement must appear within on/onClipEvent handler
     do {

Could anyone tell me what it means, and what I can do to fix it?

(layer 70, frame 2 is the place in the timeline where the snow_mc movieclip is first posted)

I did try changing the code to this:

Code:
onClipEvent (load) {
snowflakes = 100;
do {
   duplicateMovieClip(snow, "snow"+k, k);
   k++;
} while (k != snowflakes);
}

But that just makes the swf file freeze up immediately after I press "play".

EDIT: I GOT IT WORKING! YES!  Grin Grin Grin
« Last Edit: December 21, 2008, 05:13:05 AM by Esn » Logged

<a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=november%2006.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=november%2006.swf</a>
My blog about Russian animation
My Newgrounds account (animation, art, music)
My DeviantArt account (comics, photos, writing)
Brass ensemble recordings (in which I play tuba & euphonium)
Zwickel
Veteran
*****
Offline Offline

Posts: 1093


« Reply #2 on: January 23, 2009, 04:36:21 PM »

I recommend AS MAIN for thel earning of basic AS stuff, it's wonderful.
Logged
Brackenwood
   

 Logged
Pages: [1]   Go Up
Print
Jump to:  

Theme by Pieter, based on Black Rain by Crip Powered by SMF 1.1.16 | SMF © 2011, Simple Machines XHTML | CSS

Page created in 0.061 seconds with 24 queries.