gravityforce.as

来自「用于flash/flex的 as3的 2D图形图像图表的动态生成」· AS 代码 · 共 46 行

AS
46
字号
package flare.physics
{
	/**
	 * Force simulating a global gravitational pull on Particle instances.
	 */
	public class GravityForce implements IForce
	{
		private var _gx:Number;
		private var _gy:Number;
		
		/** The gravitational acceleration in the horizontal dimension. */
		public function get gravityX():Number { return _gx; }
		public function set gravityX(gx:Number):void { _gx = gx; }
		
		/** The gravitational acceleration in the vertical dimension. */
		public function get gravityY():Number { return _gy; }
		public function set gravityY(gy:Number):void { _gy = gy; }
		
		/**
		 * Creates a new gravity force with given acceleration values.
		 * @param gx the gravitational acceleration in the horizontal dimension
		 * @param gy the gravitational acceleration in the vertical dimension
		 */
		public function GravityForce(gx:Number=0, gy:Number=0) {
			_gx = gx;
			_gy = gy;
		}
		
		/**
		 * Applies this force to a simulation.
		 * @param sim the Simulation to apply the force to
		 */
		public function apply(sim:Simulation):void
		{
			if (_gx == 0 && _gy == 0) return;
			
			var p:Particle;
			for (var i:uint=0; i<sim.particles.length; ++i) {
				p = sim.particles[i];
				p.fx += _gx * p.mass;
				p.fy += _gy * p.mass;
			}
		}
		
	} // end of class GravityForce
}

⌨️ 快捷键说明

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