⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snowflake_class.as

📁 这是《Flash MX编程与创意实现》的源代码
💻 AS
字号:
/*
  SnowFlake Class v1.1
  Oct. 29, 2002
  (c) 2002 Robert Penner
  
  The SnowFlake class encapsulates functionality for a
  falling snow animation.
  Designed to collaborate with the SnowStorm class.
  
  Dependencies: 
  - Vector3d class 
  - MovieClip.addListener() for onEnterFrame events (included in core_setup.as)
  - Math.randRangeF() (included below)
  - MovieClip.drawPoly() (included below)
  - MovieClip.drawRegPoly() (included below)
  - Color.setBrightness() (included below)
  
  Discussed in Chapter 12 of 
  Robert Penner's Programming Macromedia Flash MX
  
  http://www.robertpenner.com/profmx
  http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
*/

// returns a floating number within a certain range
Math.randRangeF = function (low, high) {
	return Math.random() * (high - low) + low;
};

MovieClip.prototype.drawPoly = function (pts) {
	this.moveTo (pts[0].x, pts[0].y);
	var i = pts.length;
	while (i--) this.lineTo (pts[i].x, pts[i].y);
};

MovieClip.prototype.drawRegPoly = function (x, y, radius, numPts, rotation) {
		var angle = (-90 + rotation) * (Math.PI/180); // start at -90 deg.
		var pts = [];
		var px, py;
		var dAngle = 2 * Math.PI / numPts; // 360 deg. / numPts
		var cos = Math.cos, sin = Math.sin;
		while (numPts--) {
			angle += dAngle;
			px = radius * cos (angle) + x;
			py = radius * sin (angle) + y;
			pts.push ({x:px, y:py});	
		}
		this.drawPoly (pts);
};

// brighten just like Property Inspector
// bright between -100 and 100
Color.prototype.setBrightness = function (bright) {
	var trans = this.getTransform();
	with (trans) {
		ra = ga = ba = 100 - Math.abs (bright); // color percent
		rb = gb = bb = (bright > 0) ? bright * (256/100) : 0; // color offset
	}
	this.setTransform (trans);
};

if (typeof Vector3d != "function") trace ("Error: required class Vector3d missing");
if (typeof MovieClip.addListener != 'function') trace (">> Error: MovieClip.addListener() missing (core_setup.as)");

//////////////////////////////////////////////////////////////////////

_global.Snowflake = function (fallSpeed, timeline, depth) {
    this.mc = this.attachGraphic (timeline, depth);
    this.mcColor = new Color (this.mc);
    this.pos = new Vector3d (0, 0, 0);
    this.vel = new Vector3d (0, -fallSpeed, 0);
    this.velRot = Math.randRangeF (-10, 10);
    MovieClip.addListener (this);
};

var SFP = Snowflake.prototype;


//////////// GETTER/SETTER METHODS

// sets 3D x and y coordinates
SFP.setXY = function (x, y) {
	this.pos.x = x;
	this.pos.y = y;
	this.renderXY();
};

// sets z coordinate (distance from camera)
SFP.setZ = function (z) {
	this.pos.z = z;
	this.renderZ();
};

// sets Snowflake MC to specified _x location and sets 3D x coordinate accordingly
SFP.setScreenX = function (x) {
	this.pos.x = (this.mc._x = x) / this.pers;
};

// sets Snowflake MC to specified _y location and sets 3D y coordinate accordingly
SFP.setScreenY = function (y) {
	this.pos.y = -(this.mc._y = y) / this.pers;
};

// sets screen _x and _y and changes 3D coordinates accordingly
SFP.setScreenXY = function (x, y) {
	this.pos.x = (this.mc._x = x) / this.pers;
	this.pos.y = -(this.mc._y = y) / this.pers;
};

SFP.setStorm = function (s) {
	this.storm = s;
};

SFP.getStorm = function () {
	return this.storm;
};


//////////// PRIVATE METHODS

// attachs a movie clip to represent the Snowflake
SFP.attachGraphic = function (timeline, depth) {
	var mc = timeline.createEmptyMovieClip ("s" + depth, depth);
	with (mc) {
		beginFill (0xFFFFFF);
		drawRegPoly (0, 0, 10, 6);
		endFill();
	}
	return mc;
};

// animates the flake
SFP.move = function () {
	// change random Brownian force every 10 frames
	if (++this.t % 10 == 0) {
		this.brown = Math.randRangeF (-.2, .2);
	}
	var v = this.vel;
	// add accelerations to velocity
	v.x += this.brown + this.storm.wind;
	v.x *= 1 - this.storm.friction;
	this.pos.plus (v);
	this.rotation += this.velRot;
	this.renderXY();
};

SFP.onEnterFrame = SFP.move;

// update visuals to reflect changes in X and/or Y coordinates	
SFP.renderXY = function () {
	var mc = this.mc;
	mc._x = this.pos.x * this.pers;
	mc._y = -this.pos.y * this.pers;
	mc._rotation = this.rotation;
	// grab the limits object from the Snowstorm
	var lm = this.storm.limits;
	// check for out of bounds and wrap
	if (mc._y > lm.ymax) this.setScreenY (lm.ymin);
	if (mc._x > lm.xmax) this.setScreenX (lm.xmin)
	else if (mc._x < lm.xmin) this.setScreenX (lm.xmax);
};

// update visuals to reflect changes in Z coordinate	
SFP.renderZ = function () {	
	// amount of perspective
	this.pers = this.pos.getPerspective();
	// change z-order
	this.mc.swapDepths (Math.floor (100000 - this.pos.z * 100));
	// change scale of MC
	with (this.mc) {
		_xscale = _yscale = 100 * this.pers;
	}
	// change brightness according to distance
	(new Color (this.mc)).setBrightness (-100 * (1 - this.pers));
};


delete SFP;

trace (">> Snowflake class loaded");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -