好久未发帖了,刚才看到有人问到画sin后怎样移动所画的曲线,现举一小例,抛砖引玉...
效果如下:(并附源文件)
源文件: 画sin.rar
以下代码直接拷贝到帧上就行了:
// x0 y0 起点坐标 | k 垂直幅度 | disX 水平幅度 disY 周期 | i 速度变量
var x0:Number = 100, y0:Number = 200, k:Number = 100, disX:Number = 2, disY:Number = 1, i:Number = 0;
//=== 画水平线背景 =======================
drawViewX();
function drawViewX() {
// wh 单元格长与宽
var wh:Number = 10;
this.createEmptyMovieClip("lineX", this.getNextHighestDepth());
onEnterFrame = function () {
with (lineX) {
lineStyle(1, 0xCCCCCC, 100);
moveTo(0, wh);
lineTo(Stage.width, wh);
}
if (lineX._width>=Stage.width) {
wh += 10;
}
if (wh>=Stage.height) {
delete onEnterFrame;
drawViewY();
}
updateAfterEvent();
};
}
//=== 画垂线背景 =========================
function drawViewY() {
// wh 单元格长与宽
var wh:Number = 10;
this.createEmptyMovieClip("lineY", this.getNextHighestDepth());
onEnterFrame = function () {
with (lineY) {
lineStyle(1, 0xCCCCCC, 100);
moveTo(wh, 0);
lineTo(wh, Stage.height);
}
if (lineX._width>=Stage.height) {
wh += 10;
}
if (wh>=Stage.width) {
delete onEnterFrame;
drawX();
}
updateAfterEvent();
};
}
//======== 画sin主函数 ======================
function drawSin() {
this.createEmptyMovieClip("line", this.getNextHighestDepth());
with (line) {
lineStyle(2, 0x000000, 100);
moveTo(x0, y0);
}
var xx:Number = 0;
var yy:Number = 0;
line.onEnterFrame = function() {
xx = x0+i/disX;
yy = y0-Math.sin(i*Math.PI/180)*k;
ball._x = xx;
ball._y = yy;
line.lineTo(ball._x, ball._y);
i += 5;
if (i>disY*360) {
delete this.onEnterFrame;
ball._visible = false;
//
//==========
_root.createTextField("sxl001", _root.getNextHighestDepth(), 430, 380, 125, 20);
sxl001.background = true;
sxl001.backgroundColor = 0xFFCCFF;
sxl001.autoSize = "left";
sxl001.text = "===== sxl001 =====";
_root.createTextField("txt", _root.getNextHighestDepth(), 200, 360, 150, 20);
txt.text = "方向键控制曲线的移动";
//
Key.addListener(myListener);
}
};
}
//== 画坐标系的X轴 =========================
var v0:Number = 0;
function drawX() {
this.createEmptyMovieClip("xline", this.getNextHighestDepth());
with (xline) {
lineStyle(1, 0x000000, 100);
moveTo(20, y0);
}
xline.onEnterFrame = function() {
v0 += 20;
this.lineTo(v0, y0);
if (this._width>=Stage.width-40) {
delete this.onEnterFrame;
v0 = 0;
//== 画坐标系的Y轴
drawY();
}
};
}
//== 画坐标系的Y轴 =========================
function drawY() {
this.createEmptyMovieClip("yline", this.getNextHighestDepth());
with (yline) {
lineStyle(1, 0x000000, 100);
moveTo(x0, 20);
}
yline.onEnterFrame = function() {
v0 += 20;
this.lineTo(x0, 20+v0);
if (this._height>=Stage.height-40) {
delete this.onEnterFrame;
//显示红色小球
ballShow();
}
};
}
//== 红色小球 ====================
function ballShow() {
this.createEmptyMovieClip("ball", this.getNextHighestDepth());
with (ball) {
lineStyle(6, 0xFF0000, 100);
moveTo(0, 0);
lineTo(0.5, 0);
//= 画sin曲线
drawSin();
}
}
//== 方向键控制曲线移动 ====================
var myListener:Object = new Object();
myListener.onKeyDown = function() {
switch (Key.getCode()) {
case Key.LEFT :
line._x -= 5;
break;
case Key.RIGHT :
line._x += 5;
break;
case Key.UP :
line._y -= 5;
break;
case Key.DOWN :
line._y += 5;
break;
}
};
进入论坛和作者讨论学习:
http://space.flash8.net/bbs/thread-345685-1-1.html