开发者

Force screen redraw other than using removeChild() and then addChild() to get image to update?

开发者 https://www.devze.com 2023-04-01 23:31 出处:网络
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 displ

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


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消