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

本文介绍了一种使用Flash实现下雪效果的方法。通过编程控制雪花从天空飘落,使动画更逼真。作者利用矢量素材创建了多种雪花样式,并通过代码调整每片雪花的透明度、大小及旋转速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导读:
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值