Actionscript 3.0 程序流程

发布于 2024-11-03 21:08:05 字数 2293 浏览 0 评论 0原文

我尝试通过跟踪字符串进行尝试和错误,这样我就可以理解动作脚本(或任何类似的一般语言)的程序流程,但无济于事;我现在无法理解,或者可能是因为我没有吃早餐。请向我解释为什么跟踪语句首先显示在输出中?

这是第一帧的代码

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 并将它们注入到一个数组中,然后追踪它。

理解流程对我来说非常重要,如果不理解这一点,我不想继续前进。谢谢。

I tried trial and error by tracing strings so I could understand the program flow of actionscript(or any similar language in general) but to no avail; I couldn't understand it at this point or maybe it because I didn't eat my breakfast. Please explain to me why the trace statements showed in the output first?

Here's the code of the first Frame

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");

Second Frame:

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

Last Frame:

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

AND THE OUTPUT

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!!!

What I'm trying to do is to trigger an event when the stage is loaded; It generates some URL of images and inject them into an array then trace it back.

It is very crucial for me to understand the flow, I don't want to move forward without understanding this. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-11-10 21:08:05

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

您的程序启动,然后运行第 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 输出。

如果您理解我到目前为止所说的内容,那么其余的内容从这里就不言自明了。

希望这有帮助。

Well I hope I can break it down for you enough:

Your program starts then it runs line 3 producing the output:

I'm in line 3!

Then it gets to the following section:

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

The important thing to release here is that the above code does infact not trigger anything, instead it register a listener (some function that you have defined) to be executed when a certain event occurs. For example, the first line stage.addEventListener(Event.ENTER_FRAME, generateURLs); attaches an event listener that will fire as soon as the first frame is entered and execute your generateURLs function.

Then the program interprets line 8 and executes it:

I'm in line 8

After that you are defining the generateURLs function followed by another trace which outputs:

I'm in line 24

After that you are again defining some functions (showBar, hideBar) followed by another trace statement resulting in the:

I'm in line 34

OK now the important thing to release here is all you have done is registered some event listeners to listen to your events. However, none of your events have been fired yet which is why you haven't seen any of the trace calls execute from any of your functions. However because this is the last line on Frame 1 the program now fires the Event.ENTER_FRAME, which you have registered to listen to which in turn calls your generateURLs function resulting in the pokemon/img_XX.jpg output.

The rest is self explanatory from here if you understood what I have said so far.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文