强制屏幕重绘而不是使用removeChild()然后addChild()来更新图像?

发布于 2024-12-02 17:59:57 字数 1507 浏览 0 评论 0原文

这是我创建的一些代码作为另一个问题的答案。它将库项目从库中拉出,并以编程方式将其放在舞台上。没什么大不了的,除了为了让屏幕上显示的图像实际更新,我必须删除Child(),然后用addChild()将其返回。还有其他方法可以强制屏幕重画吗?

var currentImage:int = 1;
var MAX_IMAGES:int = 3;

MC_prevButton.addEventListener(MouseEvent.CLICK, goBack);
MC_nextButton.addEventListener(MouseEvent.CLICK, goForward);

var imageClip:MovieClip = new Image_1();
var thumbClip:MovieClip = new Thumb_1();

imageClip.x = thumbClip.x = stage.stageWidth/2;
imageClip.y = thumbClip.y = stage.stageHeight/2;



addChildAt(thumbClip, 0);
addChildAt(imageClip, 1);

setImages(currentImage);

function goBack(event:MouseEvent):void {
    if (currentImage) currentImage--;
    setImages(currentImage);
}

function goForward(event:MouseEvent):void {
    if (currentImage < MAX_IMAGES) currentImage++;
    setImages(currentImage);
}

function setImages(imageNumber){
    trace("Setting image number: " + imageNumber);

    var LibraryImage:Class = getDefinitionByName("Image_" + imageNumber) as Class;
    if (LibraryImage){
        removeChild(imageClip);
        imageClip = new LibraryImage();
        addChildAt(imageClip, 1);
    }
    var LibraryThumb:Class = getDefinitionByName("Thumb_" + imageNumber) as Class;
    if (LibraryThumb){
        removeChild(thumbClip);
        thumbClip = new LibraryThumb();
        addChildAt(thumbClip, 0);
    }

    imageClip.x = thumbClip.x = stage.stageWidth/2;
    imageClip.y = thumbClip.y = stage.stageHeight/2;
}

请注意,在此示例中,此代码位于时间线的第 1 帧上,并且它是一个 1 帧 FLA 文件

Here's some code I created as an answer to another question. It pulls a library item out of the library and puts it on stage programmatically. No big deal, except that in order to get the image displayed on the screen to actually update, I had to removeChild() and then addChild() it back. Is there another way to force the screen to redraw?

var currentImage:int = 1;
var MAX_IMAGES:int = 3;

MC_prevButton.addEventListener(MouseEvent.CLICK, goBack);
MC_nextButton.addEventListener(MouseEvent.CLICK, goForward);

var imageClip:MovieClip = new Image_1();
var thumbClip:MovieClip = new Thumb_1();

imageClip.x = thumbClip.x = stage.stageWidth/2;
imageClip.y = thumbClip.y = stage.stageHeight/2;



addChildAt(thumbClip, 0);
addChildAt(imageClip, 1);

setImages(currentImage);

function goBack(event:MouseEvent):void {
    if (currentImage) currentImage--;
    setImages(currentImage);
}

function goForward(event:MouseEvent):void {
    if (currentImage < MAX_IMAGES) currentImage++;
    setImages(currentImage);
}

function setImages(imageNumber){
    trace("Setting image number: " + imageNumber);

    var LibraryImage:Class = getDefinitionByName("Image_" + imageNumber) as Class;
    if (LibraryImage){
        removeChild(imageClip);
        imageClip = new LibraryImage();
        addChildAt(imageClip, 1);
    }
    var LibraryThumb:Class = getDefinitionByName("Thumb_" + imageNumber) as Class;
    if (LibraryThumb){
        removeChild(thumbClip);
        thumbClip = new LibraryThumb();
        addChildAt(thumbClip, 0);
    }

    imageClip.x = thumbClip.x = stage.stageWidth/2;
    imageClip.y = thumbClip.y = stage.stageHeight/2;
}

Note that in this example, this code is on frame 1 of the timeline, and that it's a 1-frame FLA file

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

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

发布评论

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

评论(1

挽清梦 2024-12-09 17:59:57

尝试在前进和后退方法的末尾调用 updateAfterEvent() :

function goForward(event:MouseEvent):void {
    if (currentImage < MAX_IMAGES) currentImage++;
    setImages(currentImage);
    event.updateAfterEvent();
}

Try calling updateAfterEvent() at the end of your forward and back methods:

function goForward(event:MouseEvent):void {
    if (currentImage < MAX_IMAGES) currentImage++;
    setImages(currentImage);
    event.updateAfterEvent();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文