我是 AS3 新手,一直致力于用 AS3 编写的 XML 驱动导航系统。
目前,我已导入 XML 文件的内容,并将其绘制在舞台上动态创建的根级别包含的 MovieClip 中。该影片剪辑称为“容器”。
我想要实现的是平滑的加速/减速效果,根据鼠标光标相对于舞台中间的位置,沿 X 轴对容器影片剪辑进行动画处理。
我的代码可以在这里找到:http://pastie.org/521432
第 87 行以后的代码是我现在用来使影片剪辑左右滚动的代码。
我所拥有的确实有效,但很笨重,但确实有效 - 我只是希望它更加精致一点,并且在谷歌中留下了空白。因为我希望即使鼠标停止移动,MovieClip 也能继续以当前相对速度滚动,所以我使用了 Timer 类的实例。
有人可以提出改进建议吗?提前致谢。
最佳答案
您应该将计算和绘图方法分开。因此,让它在 onMouseMove 处理程序中完成所有计算,但实际上在 onEnterFrame 处理程序中绘制更改。
而且我认为你的算法可以更简单,没有人会注意到。我举了一个简单的例子来说明你可以如何做到这一点。将此代码粘贴到名为 Main.as 的 AS3 文件中,并使其成为新 FLA 的文档类。
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private const boxCount:int = 10;
private const boxWidth:int = 45;
private const boxMargin:int = 5;
private const startPoint:int = 150;
private const boxesWidth:int = boxCount * (boxWidth + boxMargin);
private const endPoint:int = boxesWidth + startPoint;
private const zeroPoint:int = boxesWidth / 2 + startPoint;
private var container:MovieClip;
private var targetX:Number;
private var speed:Number = 0;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
container = new MovieClip();
addChild(container);
container.x = 150;
container.y = 300;
for (var i:int = 0; i < boxCount; i++)
{
container.graphics.beginFill(Math.random() * 0xFFFFFF);
container.graphics.drawRect(i*(boxWidth+boxMargin), 0, boxWidth, boxWidth);
}
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseMoveHandler(e:MouseEvent):void
{
var distanceFromCenter:int = stage.mouseX - zeroPoint;
speed = distanceFromCenter * -0.01; // Bring number into a good range, and invert it.
}
private function enterFrameHandler(e:Event):void
{
container.x += speed;
}
}
}
关于actionscript-3 - Flash CS4 AS3 水平影片剪辑随鼠标移动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1031986/