第三章 三角学应用Ⅱ [FL 基理译]
2009-05-30 12:08:41 来源:WEB开发网编译执行这个动画后,就得到了两个影片之间的距离。每次执行,两个影片的位置都会不同。不论它们处于什么位置,我们所获得的距离都是正数。有趣吧,但是还不够动态,在下面这个示例,可以实时地获得影片的距离,请试一下这个文档类,MouseDistance.as:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
public class MouseDistance extends Sprite {
private var sprite1:Sprite;
private var textField:TextField;
public function MouseDistance() {
init();
}
private function init():void {
sprite1 = new Sprite();
addChild(sprite1);
sprite1.graphics.beginFill(0x000000);
sprite1.graphics.drawRect(-2, -2, 4, 4);
sprite1.graphics.endFill();
sprite1.x = stage.stageWidth / 2;
sprite1.y = stage.stageHeight / 2;
textField = new TextField();
addChild(textField);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
public function onMouseMove(event:MouseEvent):void {
graphics.clear();
graphics.lineStyle(1, 0, 1);
graphics.moveTo(sprite1.x, sprite1.y);
graphics.lineTo(mouseX, mouseY);
var dx:Number = sprite1.x - mouseX;
var dy:Number = sprite1.y - mouseY;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
textField.text = dist.toString();
}
}
}
更多精彩
赞助商链接