2010-05-24 102 views
0

製作可在鼠標點擊時循環瀏覽這三個圖像的flash頁面。出於某種原因,本地的計數變更並未在全球範圍內反映出來。我試過_global,但語法很奇怪,給了我錯誤。我應該如何實現這一點?在flash中點擊增加全局變量,actionscript 3

import flash.events.Event; 

var images:Array = ["images/image.jpg", "images/image2.jpg", "images/image3.jpg"]; 
var count:int = 0; 

forward.addEventListener(MouseEvent.CLICK, loadPhoto); 


function loadPhoto(evt:Event){ 

    if(count>2){ 
     count = 0; 
    } 

    trace(count); 
    imageFrame.source = images[count]; 

    count++; 

} 

該問題的簡化版本將獲得跟蹤輸出您單擊的次數。

import flash.events.Event; 

var count:int = 0; 

forward.addEventListener(MouseEvent.CLICK, clickHandler); 

function clickHandler(evt:Event) 
{ 
    trace(count); 
    count++; 
} 

回答

1

任何機會,代碼是在一個框架中執行多次? (因爲你錯過了某個地方的停留地(例如))

如果是這樣,你可能有一個問題,你沒有注意到,這是造成這種奇怪的行爲。檢查這是否發生的一個簡單方法是在聲明count(或之前,但將其放入該框架腳本中)後添加trace("test")

+0

可能是這個問題,讓我檢查一下 – msandbot 2010-05-24 19:50:24

+0

我怎麼不把它放在一個框架中?對不起,我是新來的閃光 – msandbot 2010-05-24 19:52:07

+0

一種方法是定義一個類。把你的代碼放在那裏,並將你的Movieclip鏈接到該類庫中。 – 2010-05-24 20:03:14

1

這應該很好。我假設寫代碼的方式,這是在時間軸上,而不是在課堂上。它不應該有任何區別 -

但是嘗試用引用你的函數裏面的「計數」變量此

它可能看起來更像:

function loadPhoto(evt:Event){ 

    if(this.count>2){ 
     this.count = 0; 
    } 

    trace(this.count); 
    imageFrame.source = images[this.count]; 

    this.count++; 

} 

它更詳細和迂腐,但你的初始代碼應該工作得很好。我可能會添加 - 此代碼不會在函數外部使用'count'變量,替代聲明它 - 是問題,當函數運行時,計數器爲總是'0'?

+0

是的,這是目前的問題 – msandbot 2010-05-24 18:43:21

+0

我想跟蹤我在數組中的位置。會有按鈕前進1和返回1.他們需要知道當前正在顯示哪個圖像。現在和計數中添加的這個計數總是爲零。 – msandbot 2010-05-24 18:47:59

3

我認爲你的問題是一個範圍之一。試試這個:

import flash.events.Event; 

var images:Array = ["images/image.jpg", "images/image2.jpg", "images/image3.jpg"]; 
var count:int = 0; 

forward.addEventListener(MouseEvent.CLICK, clickHandler); 


function clickHandler(evt:Event) 
{ 
    loadPhoto(); // Notice, this is calling a function already defined on root! 
} 

function loadPhoto() 
{ 
    trace(count); 
    // Use modulous to deal with this type of behavior -- it is easier in the end. 
    imageFrame.source = images[count%images.length]; 

    count++; // Count should be within scope here. 
} 
+0

它仍然是零,我喜歡模數雖然 謝謝 – msandbot 2010-05-24 19:29:43

+0

爲好的oop編程+1。 – Glycerine 2010-05-24 20:21:42