1

我通过跟踪字符串尝试反复试验,这样我就可以理解actionscript(或任何类似的语言)的程序流程,但无济于事;在这一点上我无法理解,或者可能是因为我没有吃早餐。请向我解释为什么首先在输出中显示跟踪语句?

这是第一帧的代码

import flash.events.MouseEvent;
import flash.events.Event;

trace("I'm in line 3!");

stage.addEventListener(Event.ENTER_FRAME, generateURLs);
imageThumb.invButton.addEventListener(MouseEvent.MOUSE_OVER, showBar);
imageThumb.invButton.addEventListener(MouseEvent.MOUSE_OUT, hideBar);

trace("I'm in line 8");
// Generates the image URLs and inject them to the imageURLs array
var imageURLs:Array = new Array();
function generateURLs(e:Event):void {
    trace("I'm inside the generateURLs function!");
    var url:String = new String();
     for(var i:int = 0; i <= 31; i++) {
        url = new String('pokemon/img_' + i);
        imageURLs.push(url + ".jpg");
        trace(imageURLs[i]);
     }
     stage.removeEventListener(Event.ENTER_FRAME, generateURLs);
}


trace("I'm in line 24");

function showBar(evt:MouseEvent):void {
    trace("I'm inside the ShowBar function!");
    imageThumb.bar.gotoAndPlay('over');
}
function hideBar(evt:MouseEvent):void {
    trace("I'm inside the hideBar function!");
    imageThumb.bar.gotoAndPlay('out');
}

trace("I'm in line 34");

第二帧:

trace("We're not in Frame 1 anymore!");

最后一帧:

stop();
trace("STOP!!!");

和输出

I'm in line 3!
I'm in line 8
I'm in line 24
I'm in line 34
I'm inside the generateURLs function!
pokemon/img_0.jpg
pokemon/img_1.jpg
pokemon/img_2.jpg
pokemon/img_3.jpg
pokemon/img_4.jpg
pokemon/img_5.jpg
pokemon/img_6.jpg
pokemon/img_7.jpg
pokemon/img_8.jpg
pokemon/img_9.jpg
pokemon/img_10.jpg
pokemon/img_11.jpg
pokemon/img_12.jpg
pokemon/img_13.jpg
pokemon/img_14.jpg
pokemon/img_15.jpg
pokemon/img_16.jpg
pokemon/img_17.jpg
pokemon/img_18.jpg
pokemon/img_19.jpg
pokemon/img_20.jpg
pokemon/img_21.jpg
pokemon/img_22.jpg
pokemon/img_23.jpg
pokemon/img_24.jpg
pokemon/img_25.jpg
pokemon/img_26.jpg
pokemon/img_27.jpg
pokemon/img_28.jpg
pokemon/img_29.jpg
pokemon/img_30.jpg
pokemon/img_31.jpg
We're not in Frame 1 anymore!
STOP!!!

我要做的是在加载舞台时触发一个事件;它生成一些图像的 URL 并将它们注入到一个数组中,然后追溯它。

了解流程对我来说非常重要,我不想在不了解这一点的情况下继续前进。谢谢你。

4

1 回答 1

1

好吧,我希望我可以为您分解它:

您的程序启动,然后运行第 3 行,产生输出:

I'm in line 3!

然后它进入以下部分:

stage.addEventListener(Event.ENTER_FRAME, generateURLs);
imageThumb.invButton.addEventListener(MouseEvent.MOUSE_OVER, showBar);
imageThumb.invButton.addEventListener(MouseEvent.MOUSE_OUT, hideBar);

这里要发布的重要一点是,上面的代码实际上并没有触发任何事情,而是注册了一个侦听器(您已定义的某个函数)以在某个事件发生时执行。例如第一行stage.addEventListener(Event.ENTER_FRAME, generateURLs); 附加一个事件侦听器,该侦听器将在输入第一帧后立即触发并执行您的generateURLs函数。

然后程序解释第 8 行并执行它:

I'm in line 8

之后,您将定义generateURLs 函数,然后定义另一个输出:

I'm in line 24

之后,您再次定义一些函数(showBarhideBar),然后是另一个跟踪语句,导致:

I'm in line 34

好的,现在要在这里发布的重要事情是您所做的就是注册一些事件侦听器来侦听您的事件。但是,您的任何事件都没有被触发,这就是为什么您没有看到任何跟踪调用从您的任何函数执行的原因。然而,因为这是第 1 帧的最后一行,程序现在触发Event.ENTER_FRAME,您已注册监听该事件,然后调用您的generateURLs函数,从而产生pokemon/img_XX.jpg输出。

如果您理解我到目前为止所说的话,其余的就从这里开始自我解释了。

希望这可以帮助。

于 2011-04-30T03:22:59.943 回答