📄 physics_particle_class.as
字号:
/*
PhysicsParticle Class v1.1
Oct. 29, 2002
(c) 2002 Robert Penner
This is a custom object that controls a movie clip,
turning it into particle driven by physics forces.
Designed to collaborate with Force class (and subclasses).
Dependencies:
- Vector class
- MovieClip.addListener() (included in core_setup.as)
Discussed in Chapter 11 of
Robert Penner's Programming Macromedia Flash MX
http://www.robertpenner.com/profmx
http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
*/
if (typeof _global.Vector != 'function') trace (">> Error: required class missing - Vector");
_global.PhysicsParticle = function (target, x, y, xProp, yProp) {
this.target = target;
this.pos = new Vector (x, y);
this.vel = new Vector (0, 0);
this.accel = new Vector (0, 0);
this.friction = new Vector (0, 0);
this.setXYProps (xProp, yProp);
this.forces = {};
MovieClip.addListener (this);
};
var PPP = PhysicsParticle.prototype;
//////// PUBLIC METHODS
PPP.addForce = function (id_str, force_obj) {
this.forces[id_str] = force_obj;
force_obj.setParent (this);
};
PPP.removeForce = function (id_str) {
delete this.forces[id_str];
};
//////// GETTER/SETTER METHODS
PPP.setPosition = function (x, y) {
this.pos.reset (x, y);
};
PPP.getPosition = function () {
return this.pos;
};
PPP.setFriction = function (fx, fy) {
if (fy == undefined) fy = fx;
this.friction.reset (fx, fy);
};
PPP.getFriction = function () {
return this.friction;
};
PPP.setXYProps = function (xp, yp) {
this.xProp = (xp == undefined) ? "_x" : xp;
this.yProp = (yp == undefined) ? "_y" : yp;
};
//////// PRIVATE METHODS
PPP.move = function () {
var forceX = 0, forceY = 0;
var fs = this.forces;
for (var i in fs) {
if (!fs[i].live()) {
delete fs[i];
} else {
forceX += fs[i].value.x;
forceY += fs[i].value.y;
}
}
this.accel.reset (forceX, forceY);
var v = this.vel;
v.plus (this.accel);
v.x *= (1 - this.friction.x);
v.y *= (1 - this.friction.y);
this.pos.plus (v);
this.render();
};
PPP.render = function () {
this.target[this.xProp] = this.pos.x;
this.target[this.yProp] = this.pos.y;
};
PPP.onEnterFrame = PPP.move;
delete PPP;
trace (">> PhysicsParticle class loaded");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -