1

我使用 Actionscript 2 在 Flash 中创建了一个图像滑块。基本上我有五个 600 像素的图像并排堆叠在一个名为 ContentMC 的剪辑中。目前,当按下相应的按钮时,滑块可以很好地在图像之间滚动,但我想弄清楚的是如何让图像每隔几秒就自动滚动一次,而不需要用户交互。我在主时间轴上的操作层上有以下代码:

import mx.transitions.Tween;
import mx.transitions.easing.*;

btn1.endX = 64;
btn1.onPress = doPress;
btn2.endX = -536;
btn2.onPress = doPress;
btn3.endX = -1136;
btn3.onPress = doPress;
btn4.endX = -1736;
btn4.onPress = doPress;
btn5.endX = -2336;
btn5.onPress = doPress;

function doPress() {
    var twX = new Tween(contentMC, "_x", Strong.easeInOut, contentMC._x, this.endX, 1.5, true);
}

我有一个名为 contentMC 的实例,其中包含所有图像。我有 5 个名为 btn1 到 btn5 的实例,每个实例都包含按钮影片剪辑。

同样,我正在寻求帮助的特定功能是让图像每隔几秒钟自动滑动一次(以及通过点击相应的按钮保留滑动到特定图像的能力)。对于我的任何无知,我深表歉意,但我对 Actionscript 不太精通。

谢谢大家。

4

1 回答 1

0

好的,我的动作脚本有点生疏,但我想你会想尝试这样的事情。

import mx.transitions.Tween;
import mx.transitions.easing.*;

// Initialize Pixel Offsets in an array
// This is the equivalent of what you were doing before but more accessible 
// to code. It has to start at 0 though, so btn1.endX is offsetArray[0] and 
// btn2.endX is offsetArray[1] and so on and so on.
var offsetArray = new Array();
offsetArray[0] = 64;
offsetArray[1] = -536;
offsetArray[2] = -1136;
offsetArray[3] = -1736;
offsetArray[4] = -2336;

// Setup button callbacks
btn1.onPress = function() { changeImage(1); }
btn2.onPress = function() { changeImage(2); }
btn3.onPress = function() { changeImage(3); }
btn4.onPress = function() { changeImage(4); }
btn5.onPress = function() { changeImage(5); }

// Array of buttons, used to dynamically set buttons to down state
var btnArray = new Array();
btnArray[0] = btn1;
btnArray[1] = btn2;
btnArray[2] = btn3;
btnArray[3] = btn4;
btnArray[4] = btn5;

// Button one down by default
btnArray[0].gotoAndStop(3);

// Setup the Timer
// Change this value to alter the delay in image changes
secondDelay = 4;
totalDelay = 30;
// Number of images in the slider (Don't change this unless you add more onPress and endX variables above)
totalImages = 5;

// Don't change these
secondCounter = 0;
totalCounter = 0;
var imageCounter = 1;
// Fires the delay function every 1000 milliseconds
var imageTimer = setInterval(delay, 1000);

// General Function for changing an image in the slider
function changeImage(offset) {
    imageCounter = offset;
    clearInterval(imageTimer);

    if (totalCounter < totalDelay) {
        secondCounter=0;
        imageTimer = setInterval(delay, 1000);
        // Make sure we don't try to move to an image that doesn't exist
        if (imageCounter > totalImages) {
             imageCounter = 1;
        }
    }

    var twX = new Tween(contentMC, "_x", Strong.easeInOut, contentMC._x, offsetArray[imageCounter - 1], 1.5, true);

    // Reset buttons
    for (var i=0;i<totalImages;i++) {
        btnArray[i].gotoAndStop(1);
    }
    // Set button to downstate
    btnArray[imageCounter - 1].gotoAndStop(3);
}

// Timer function for dealing with the passing of time
function delay() {
    secondCounter++;
    totalCounter++;

    if (totalCounter > totalDelay) {
        clearInterval(imageTimer);
    }

    // If it is time to do something, we can do it!
    if (secondCounter >= secondDelay) {
        secondCounter = 0;
        imageCounter++;
        // Now call our changeImage function with the correct offset value
        changeImage(imageCounter);
    }
}

// Script for button animations
btn1.onRollOver = function() {
btn1.gotoAndPlay(2);
}
btn1.onRollOut = function() {
    if (imageCounter == 1) {
        btn1.gotoAndStop(3);
    } else {
        btn1.gotoAndStop(1);
    }
}

btn2.onRollOver = function() {
btn2.gotoAndPlay(2);
}
btn2.onRollOut = function() {
    if (imageCounter == 2) {
        btn2.gotoAndStop(3);
    } else {
        btn2.gotoAndStop(1);
    }
}

btn3.onRollOver = function() {
btn3.gotoAndPlay(2);
}
btn3.onRollOut = function() {
    if (imageCounter == 3) {
        btn3.gotoAndStop(3);
    } else {
        btn3.gotoAndStop(1);
    }
}

btn4.onRollOver = function() {
btn4.gotoAndPlay(2);
}
btn4.onRollOut = function() {
    if (imageCounter == 4) {
        btn4.gotoAndStop(3);
    } else {
        btn4.gotoAndStop(1);
    }
}

btn5.onRollOver = function() {
btn5.gotoAndPlay(2);
}
btn5.onRollOut = function() {
    if (imageCounter == 5) {
        btn5.gotoAndStop(3);
    } else {
        btn5.gotoAndStop(1);
    }
}

这绝对可以重构,因为它完成得有点快,但它应该让任何人开始。

不幸的是,我没有可用的 flash studio 来测试它,但这应该是一个好的开始。如果您有任何问题或疑问,请向我发表评论。

于 2012-04-26T20:32:34.717 回答