您的位置:首页 > 其它

【翻译】使用AS3创建雪花效果

2007-12-20 21:16 357 查看
导读:
  How Does It Work?

我很生气,翻了一遍的文章在‘剪影’里发表的时候死了,我的心血付之东流,因此第二遍再转的时候,便不准备再翻译了,我很讨厌重复劳动,提示大家一定要到原文的页面去看看漂亮的效果
  The artwork I got from a stock photo site. I found a bunch of cool vector snowflakes and trees and stuff and I used them to put together the graphics for the movie. It’s really a pretty basic animation, but I thought I’d have some fun with the snowflakes and actually program them falling from the sky so it is more realistic. It may sound more difficult, but it honestly took me a lot less time than it would have if I actually did a bunch of animations for all of them. As well, I can now just change a number to set the number of snowflakes to fall on the movie, and what different types of snowflakes to use from the library.
  The Falling Snowflake Code
class com.netshiftmedia.Snowflake {
private var _snowflake:MovieClip;
private var _ranSnowflake:Number;
private var i:Number;
private var k:Number;
private var rad:Number;
private static var NUM_SNOWFLAKE_TYPES:Number=7;
private static var MOVIE_WIDTH:Number=600;
private static var MOVIE_HEIGHT:Number=400;
private static var FALLING_SPEED:Number=50;
private static var WIND_SPEED:Number=5;
private static var ROTATION_SPEED:Number=4;
function Snowflake(container:MovieClip) {
this._ranSnowflake=Math.round((Math.random()*Snowflake.NUM_SNOWFLAKE_TYPES)+1);
this._snowflake=container.attachMovie("snowflake"+this._ranSnowflake,"snowflake",container.getNextHighestDepth());
this._snowflake._x=(Math.random()*Snowflake.MOVIE_WIDTH);
this._snowflake._y=0;
this._snowflake.parent=this;
this.i=1+Math.random()*2;
this.k=-Math.PI+Math.random()*Math.PI;
this.rad=0;
//giving each snowflake unique characteristics
this._snowflake._xscale = this._snowflake._yscale=Math.random()*30;
this._snowflake._alpha = 75+Math.random()*100;
this._snowflake.onEnterFrame=function() {this.parent.snowflakeEnterFrame(this._snowflake);}
}
public function snowflakeEnterFrame() {
//putting it all together
this.rad += (k/180)*Math.PI;
this._snowflake._x -= Math.cos(rad);
this._snowflake._y += i;
if (this._snowflake._y>=Snowflake.MOVIE_HEIGHT) {
this._snowflake._y = -Snowflake.FALLING_SPEED;
}
if ((this._snowflake._x>=Snowflake.MOVIE_WIDTH) || (this._snowflake._x<=0)) {
this._snowflake._x = -Snowflake.WIND_SPEED+Math.random()*Snowflake.MOVIE_WIDTH;
this._snowflake._y = -Snowflake.WIND_SPEED;
}
this._snowflake._rotation+=Snowflake.ROTATION_SPEED;
}
}

  It’s pretty straightforward, the movie consists of one class, the Snowflake, and there are 7 different snowflake types in the .fla library. Each with their linkage identifier set at “snowflake1″, “snowflake2″ and so on.
  


  When the class is instantiated it grabs a random snowflake from the library and attaches it to the movieclip. Then it sets the alpha and scale so each snowflake is unique and then the onEnterFrame event starts which actually animates the snowflake.
  The onEnterFrame function checks the height and width of the movie so that the snowflakes don’t fall off stage, then it adjusts the x and y values according to the falling speed variable you set and a random number K. I found that the snowflakes looked pretty lame at this point, so I added some rotation in to make them more realistic. I could go further and have the rotation speed become variable to the adjustment of the x coordinate but I thought it looked pretty cool at this point.
  Creating the Snowflakes
  It is really simple to create these snowflakes on the stage, at certain frames throughout the main animation, I just added:
  
import com.netshiftmedia.*;
for (var i:Number=0;i<30;i++) {
var snowflake=new Snowflake(this);
}

  The import statement only has to be used at the beginning of the movie, but the for loop goes through and instantiates 30 snowflakes in this case. So at this point it creates 30 snowflakes (from a random selection of 7 snowflake graphics in the library), positions them at different x coordinates on the stage and sets them to a unique alpha and scale so every snowflake is different. I’ve placed these loops at about ever 50 or so frames in the animation so there is a lot of snowflakes but it doesn’t seem to slow it down at all. I’ve found that animating things in code this way provides better overall performance to the flash player than actually animating it on the timeline. Plus it’s a lot more fun.

本文转自
http://www.jarrodgoddard.com/blog/seasons-greetings
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: