As3弹簧算法
package com.klstudio.algorithms {
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
/**
* 弹簧算法
* @author Kinglong(kinglong@gmail.com)
* @since:2010-9-20
*/
[SWF(width = 550, height = 400, frameRate = 24, backgroundColor = 0x333333)]
public class Spring extends Sprite {
/**
* 显示元件
*/
private var _icon:MovieClip;
/**
* xy速度
*/
private var _vx:Number;
private var _vy:Number;
/**
* xy加速度
*/
private var _ax:Number;
private var _ay:Number;
/**
* 弹簧强度
*/
private var _k:Number;
/**
* 阻尼系数
*/
private var _damp:Number;
public function Spring() {
_icon = new jAcid_smiley();
addChild(_icon);
initParams();
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function initParams():void{
_vx = _vy = 0;
_k = 0.1;
_damp = 0.95;
}
private function enterFrameHandler(event:Event):void{
_ax = (mouseX - _icon.x) * _k;
_ay = (mouseY - _icon.y) * _k;
_vx += _ax;
_vy += _ay;
_icon.x += _vx;
_icon.y += _vy;
_vx *= _damp;
_vy *= _damp;
}
}
文章来源:http://www.hulian.top,转载请注明!