0

我有这段代码,我在网上找到了一个无限旋转的画廊,现在我的问题是在进入帧时它会跳跃并且太快。我希望它在您将鼠标悬停在徽标之外后一样快。

这是代码:

//Import TweenMax
import com.greensock.TweenMax;

//Save the horizontal center
var centerX:Number = stage.stageWidth / 2;

//Save the width of the whole gallery
var galleryWidth:Number = infiniteGallery.width;

//Speed of the movement (calculated by the mouse position in the moveGallery() function)
var speed:Number = 0;

//Add an ENTER_FRAME listener for the animation
addEventListener(Event.ENTER_FRAME, moveGallery);

function moveGallery(e:Event):void {

 //Calculate the new speed
 speed = -(0.02 * (mouseX - centerX));

 //Update the x coordinate
 infiniteGallery.x+=speed;

 //Check if we are too far on the right (no more stuff on the left edge)
 if (infiniteGallery.x>0) {

  //Update the gallery's coordinates
  infiniteGallery.x= (-galleryWidth/2);
 }

 //Check if we are too far on the left (no more stuff on the right edge)
 if (infiniteGallery.x<(-galleryWidth/2)) {

  //Update the gallery's coordinates
  infiniteGallery.x=0;
 }
}

这是演示 »

4

1 回答 1

2

滚动条的速度取决于三件事:

1-帧速率。ENTER_FRAME 事件处理程序在每一帧上都被调用,因此直接受帧速率的影响。

2- 速度阻尼数。在您的情况下,这等于 0.02。如果您想减慢滚动速度,请将其设为较小的数字。尝试 0.01 一半的速度。

3- 鼠标指针到舞台中心 x 坐标的距离。鼠标指针和舞台中心之间的差异乘以您的速度阻尼数。如果您希望运动在您处于或围绕中心时停止,请按如下方式编写代码:

var buffer:Number = 50;

function moveGallery(e:Event):void {
    var diff = mouseX - centerX;
    if (Math.abs(diff) > buffer) 
        speed = -(0.02 * (diff + (diff > 0 ? -buffer : buffer)));
    else
        speed = 0;

按照要求:

var centerX:Number = stage.stageWidth / 2;
var galleryWidth:Number = infiniteGallery.width;
var speed:Number = 0;
addEventListener(Event.ENTER_FRAME, moveGallery);

var buffer:Number = 100;

function moveGallery(e:Event):void {
    var diff = mouseX - centerX;
    if (Math.abs(diff) > buffer) 
        speed = -(0.02 * (diff + (diff > 0 ? -buffer : buffer)));
    else
        speed = 0;

    infiniteGallery.x += speed;

    if (infiniteGallery.x>0) {
        infiniteGallery.x = -galleryWidth / 2;
    }

    if (infiniteGallery.x < -galleryWidth / 2) {
        infiniteGallery.x = 0;
    }
}
于 2010-01-20T16:50:11.023 回答