如何在不设置 AS3 可见性的情况下显示大量图像?
How to show a lot of images without setting visibility in AS3?
我做了一个问答游戏,我希望能够在用户回答正确后立即显示图像。问题是我有一堆问题和图片,为每张图片设置可见性变得很乏味。我如何优化这个过程。我在想也许将图像放在一个数组中,但我真的不知道它是否可能或让它显示在我想要的地方。
据我了解,问题是您有 N 个图像,并且每次都在整个图像集上进行迭代以设置可见性。在您的情况下,我会(如您所建议的那样)创建这些图像的数组和一些辅助函数。一些基本示例:
private var imageVector: Vector.<DisplayObject>; // this vector holds all your images
private var currentImage: DisplayObject; // the image that is shown currently
private function createAndFillImages():void {
imageVector = new Vector.<DisplayObject>();
imageVector.push(image1);
imageVector.push(image2);
//... etc. it depends on how your images are presented.
}
private function onAnswerGiven():void {
const img: DisplayObject = ... // pick the right image here
showImage(img)
}
private function showImage(img: DisplayObject):void {
if (currentImage != null) currentImage.visible = false;
currentImage = img;
// ... do the positioning here
currentImage.visible = true;
}
我做了一个问答游戏,我希望能够在用户回答正确后立即显示图像。问题是我有一堆问题和图片,为每张图片设置可见性变得很乏味。我如何优化这个过程。我在想也许将图像放在一个数组中,但我真的不知道它是否可能或让它显示在我想要的地方。
据我了解,问题是您有 N 个图像,并且每次都在整个图像集上进行迭代以设置可见性。在您的情况下,我会(如您所建议的那样)创建这些图像的数组和一些辅助函数。一些基本示例:
private var imageVector: Vector.<DisplayObject>; // this vector holds all your images
private var currentImage: DisplayObject; // the image that is shown currently
private function createAndFillImages():void {
imageVector = new Vector.<DisplayObject>();
imageVector.push(image1);
imageVector.push(image2);
//... etc. it depends on how your images are presented.
}
private function onAnswerGiven():void {
const img: DisplayObject = ... // pick the right image here
showImage(img)
}
private function showImage(img: DisplayObject):void {
if (currentImage != null) currentImage.visible = false;
currentImage = img;
// ... do the positioning here
currentImage.visible = true;
}