AS3 将多个外部图像加载到数组中?
尝试随机选取 4 个图像并将它们加载到数组中,然后使用计时器显示它们,当显示所有 4 个图像时,将再次加载另外 4 个图像。这是代码:
var images : Array = new Array();
var rndNumbers : Array = new Array();
var imageLoader : Loader;
var imageTimer : Timer = new Timer(3000, 0);
var currImageID : int;
var imgID : int;
var loaded : Boolean = true;
var i : int;
var tmp : int = 0;
var rnd : int = 0;
addEventListener(Event.ENTER_FRAME, OnLoad);
imageTimer.addEventListener(TimerEvent.TIMER, ChangePicture);
function OnLoad(e : Event) : void {
RandomNumbers();
LoadImages();
}
function RandomNumbers() {
for (var n = 0; n <= 3; n++) {
rnd = 1 + Math.floor(Math.random() * 4);
while (tmp == rnd) {
rnd = 1 + Math.floor(Math.random() * 4);
}
tmp = rnd;
rndNumbers[n] = rnd;
trace(rnd);
}
}
function LoadImages() : void {
for (var i = 0; i <= rndNumbers.length - 1; i++) {
imageLoader = new Loader;
var urlRequest : URLRequest = new URLRequest("images/pic" + rndNumbers[i] + ".jpg");
imageLoader.load(urlRequest);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
}
imageTimer.start();
}
function LoadComplete(event : Event) {
removeEventListener(Event.ENTER_FRAME, OnLoad);
images[imgID] = imageLoader;
imgID++;
}
function ChangePicture(event : TimerEvent) : void {
transition.gotoAndPlay(1);
Img_Box.addChild(images[currImageID]);
if (currImageID != 3) {
currImageID++;
} else {
RandomNumbers();
LoadImages();
currImageID = 0;
}
}
这里可能的问题是加载图像的 for 循环不会等待加载完成并继续循环。这该如何解决呢?或者您有什么更好的方法建议吗?
提前致谢。
Trying to pick 4 images randomly and load them into and array and then show them using a timer when all 4 images are shown 4 more images will be loaded again. here is the code:
var images : Array = new Array();
var rndNumbers : Array = new Array();
var imageLoader : Loader;
var imageTimer : Timer = new Timer(3000, 0);
var currImageID : int;
var imgID : int;
var loaded : Boolean = true;
var i : int;
var tmp : int = 0;
var rnd : int = 0;
addEventListener(Event.ENTER_FRAME, OnLoad);
imageTimer.addEventListener(TimerEvent.TIMER, ChangePicture);
function OnLoad(e : Event) : void {
RandomNumbers();
LoadImages();
}
function RandomNumbers() {
for (var n = 0; n <= 3; n++) {
rnd = 1 + Math.floor(Math.random() * 4);
while (tmp == rnd) {
rnd = 1 + Math.floor(Math.random() * 4);
}
tmp = rnd;
rndNumbers[n] = rnd;
trace(rnd);
}
}
function LoadImages() : void {
for (var i = 0; i <= rndNumbers.length - 1; i++) {
imageLoader = new Loader;
var urlRequest : URLRequest = new URLRequest("images/pic" + rndNumbers[i] + ".jpg");
imageLoader.load(urlRequest);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
}
imageTimer.start();
}
function LoadComplete(event : Event) {
removeEventListener(Event.ENTER_FRAME, OnLoad);
images[imgID] = imageLoader;
imgID++;
}
function ChangePicture(event : TimerEvent) : void {
transition.gotoAndPlay(1);
Img_Box.addChild(images[currImageID]);
if (currImageID != 3) {
currImageID++;
} else {
RandomNumbers();
LoadImages();
currImageID = 0;
}
}
The possible problem here would be that the for loop which loads images won't wait for the load to complete and continues the loop whatsoever. How is can that be solved? Or do you suggest any better way?
Thanks in advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,照片库上没有视频教程,但这里有一个关于如何制作照片的 Actionscript 3 教程画廊。由于您是 AS3 的新手,因此请从这里开始,然后考虑使用 AS3 中的计时器来切换照片。我的建议也是:尝试避免使用时间线补间和动画,尝试使用纯动作脚本。这是因为闪存基本上是一个存在身份危机的工具/平台。它旨在供艺术家和动画师(因此是时间线、设计师 UI)以及开发人员使用。当你将这两个世界融合在一起,然后进入更复杂的项目时,事情很快就会变得一团糟。但是,如果您按照代码中的暗示进行简单的转换,这应该没问题。
确保在学习时学习了 ActionScript 3。 ActionScript 2 是旧的 Flash AVM 语言(ActionScript 虚拟机 1),它比 ActionScript 3 慢大约 20 倍,是一种非类型安全或非严格语言。 Actionscript 3 是一种基于 ecmascript 3 语言标准的类型安全严格语言,并且一度是 ecma 脚本版本 4.0 格式提案的基本模型。然而,由于像微软这样的人,它作为新规范被关闭,并因此被认为是一种专有语言,但仍然基于开放标准。
至于您的代码,您可以简单地更改 onLoad 方法,以通过类成员变量来记录 onLoad 回调被调用的次数。一旦计数 4 次(完成 4 次加载),您就可以将变量重置回 0。此外,您可以将该函数更改为通用函数,然后从内部调用它,而不是在 ENTER_FRAME 事件中放置第一个加载调用该动作脚本所在的框架。 (我假设此代码只是在闪存 FLA 中的帧上键入)。示例:
如果您想成为一名认真的、面向对象的 Flash 开发人员,您还需要学习如何开始使用类,包括文档类。您可以在这里找到有关此内容的视频教程的链接:
http://gotoandlearn.com/play。 php?id=43
该网站还包含大量免费视频教程,这些教程应该可以帮助您顺利成为一名成熟的 Flash 开发人员。我自己在 Flash 方面的基础是通过观看这些教程刚发布时严格掌握的。从那里您可能想要查看有关面向对象编程和设计模式的书籍/教程,以及它们如何应用于 ActionScript 3 语言。希望这对您有所帮助,并祝您一切顺利。
Okay so no video tutorial on the photo gallery but here is an Actionscript 3 tutorial on how to make a photo gallery. Since you're new to AS3, start there and then look into using Timers in AS3 to switch up your photos. Also my advice: try and avoid doing timeline tweens and animations, try and go pure actionscript. This is because basically flash is a tool/platform with an identity crisis. It's meant to be used for artists and animators (hence the timeline stuff, designer UI) and also developers. When you blend the two worlds, and then get into more complex projects, things get messed up really fast. But if you're doing simple transitions as is implied in your code this should be okay.
Make sure when learning, you learn actionscript 3. Actionscript 2 is the old flash AVM language (actionscript virtual machine 1) and its about 20X slower than actionscript 3, is a non-type-safe or non-strict language. Actionscript 3 is a type-safe strict language based on ecmascript 3 language standards and was at one point the base model for the ecma script version 4.0 format proposition. It was however shut down as the new spec thanks to people like microsoft and is considered a proprietary language because of this, but is yet still based on open standards.
As for your code you can simply change the onLoad method to keep a count via a class-member variable of how many times the onLoad callback has been called. Once you count 4 times (4 loads completed), you can then reset the variable back to 0. Also instead of having the first load calls placed inside an ENTER_FRAME event, you can change the function to a generic function and just call it from inside the frame this actionscript is placed on. (I'm assuming this code is just typed on a frame in the flash FLA). Example:
Also you're going to want to learn how to start using classes, including the document class if you want to become a serious, object-oriented flash developer. You can find a link to a video tutorial about this here:
http://gotoandlearn.com/play.php?id=43
That website is also packed full of free video tutorials that should have you well on your way to becoming a full-blown flash developer. I myself got my foundation in flash strictly from watching these tutorials when they were first coming out. From there you may want to look into books/tutorials on object-oriented programming and design patters, and how they apply to the actionscript 3 language. Hope this helps and all the best on your endeavors.