2012-04-25 65 views
1

我已經使用ActionScript 2在Flash中創建了一個圖像滑塊。基本上我有五個600 px圖像堆疊在一個名爲ContentMC的剪輯旁邊。目前,當按下相應的按鈕時,滑塊可以很好地在圖像之間滾動,但是我想知道的是如何讓圖像每隔幾秒滾動一次,而不需要用戶進行交互。我對主時間軸的動作層上下面的代碼:Actionscript 2圖像滑塊時機

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個實例通過btn5調用btn1,每個實例都包含按鈕影片剪輯。

此外,我正在尋找幫助的特定功能是讓圖像每隔幾秒自行滑動(並保留通過點擊相應按鈕可滑動到特定圖像的功能)。我爲我的任何無知道歉,但我不太熟悉Actionscript。

謝謝大家提前。

回答

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可以測試這個,但它應該是一個好的開始。如果您有任何問題或疑問,請發表評論。

+0

好吧,這很好,非常好。只有兩個問題: 1.它從圖像5開始,然後滾動到圖像2,然後正常執行序列。因此,出於某種原因而不是從第一張圖像開始,它會從最後一張圖像開始,然後跳過第一張圖像。這隻會在第一次通過序列時發生,之後就很好。 2.切換圖像的按鈕不再有效。 最後,如果我想在這段時間內停止滾動它自己,我該怎麼做? – 2012-04-26 20:42:18

+0

Coderates爲我糾正了這段代碼,並將它提供給我。他的迴應中的代碼是100%正確的,功能比我所要求的要好得多。非常感謝Coderates。 – 2012-04-27 15:59:09