发布网友 发布时间:2022-05-10 20:09
共1个回答
热心网友 时间:2023-04-25 18:27
//这个例子是鱼儿随鼠标自由游动的例子 //初始化,创建数组 var N = 20; var x = new Array(); var y = new Array(); var fish = new Array(); //设置鱼的初始位置 for(var i = 0; i < N; i++) { x[i] = 300; y[i] = 200; } for(i = N - 1; i >= 0; i--) { var mc:MovieClip; if(i == 0) mc = attachMovie("鱼头", "fish_" + i, this.getNextHighestDepth()); else if(i == 5) mc = attachMovie("鱼鳍", "fish_" + i, this.getNextHighestDepth()); else mc = attachMovie("鱼尾", "fish_" + i, this.getNextHighestDepth()); mc._x = x[i]; mc._y = y[i]; mc._alpha = 100 * (1 - i / N); mc._xscale = mc._yscale = 100 - i * 2; fish[i] = mc; } var dest_x = _xmouse; var dest_y = _ymouse; var mouseListener = new Object(); mouseListener.onMouseMove = function(){ dest_x = _xmouse; dest_y = _ymouse; }; Mouse.addListener(mouseListener); this.onEnterFrame = function() { if(Math.abs(dest_x - x[0]) < 20 && Math.abs(dest_y - y[0]) < 20) { dest_x = _xmouse + Math.round(Math.random() * 300 - 150); dest_y = _ymouse + Math.round(Math.random() * 200 - 100); } //更新位置坐标 x[0] += (dest_x - x[0]) * 0.1; y[0] += (dest_y - y[0]) * 0.1; for(var i = 1; i < N; i++) { x[i] += (x[i - 1] - x[i]) / 2; y[i] += (y[i - 1] - y[i]) / 2; } for(i = 0; i < N; i++) { fish[i]._x = x[i]; fish[i]._y = y[i]; fish[i]._rotation = Math.atan2(y[i] - y[i - 1], x[i] - x[i - 1]) * 180 / Math.PI; } fish[0]._rotation = fish[1]._rotation; };