hreimer
hreimer

Reputation: 784

as3 wait until image loaded on button click

I have a slideshow that loads image files with the BulkLoader class from here. When the first image is loaded completely, it gets added to a MovieClip container to add it to the stage.. and it gets displayed fullscreen, but only if a button is clicked:

fullScreenButton.addEventListener(MouseEvent.CLICK, showFull);

function showFull(e:MouseEvent):void {   
    stage.addChild(mcSlideHolder);
    ...
}

Loading the image with BulkLoader in a separate function loadAllSlides() that gets called right from the start:

imageLoader = new BulkLoader("imgLoader");
imageLoader.add(new URLRequest(paths[0]), {id:"slide_0});                                
imageLoader.get("slide_0).addEventListener(Event.COMPLETE, onFirstSlideLoaded);
imageLoader.start(1);

And during the loading process:

function onFirstSlideLoaded(e:Event):void
{
    firstImage = imageLoader.getBitmap(e.currentTarget.id);
    currentImage.addChild(firstImage);
    mcSlideHolder.addChild(currentImage); 
    mcSlideHolder.dispatchEvent(new Event("firstImgAdd"));
}

This works fine unless the fullScreenButton is clicked before the image is loaded completely, then of course nothing is visible. How do I get button click event that calls showFull() to wait until the image is loaded/added to the container or how can I add it again? What is a possible/the best approach?

I tried dispatching a custom event "firstImgAdd" that adds the image again to the container when the fullScreenButton is clicked, as well as with a try/catch block or even a simple if condition but none of them worked, meaning the image does not get added to the container after it has finished loading when the button is clicked too early. Any help is appreciated, thanks in advance

Upvotes: 1

Views: 1181

Answers (2)

The_asMan
The_asMan

Reputation: 6402

This is a security violation.
You can NOT instantiate full screen mode without user interaction.

The ONLY way to activate full screen is directly from user interaction period.IE:a mouse click
Look at it this way, any hacker trying to force a user to watch their ad full screen and not have a way out because the user shut full screen down the hacker could just reopen it.

The closest you can get to full screen without user interaction would be to expand the SWF to the full size of the HTML document.

Sorry to burst your bubble I have myself tried to hack around this also. An actual user click event has to be in the stack for you to be able to go full screen

Upvotes: 0

Corey
Corey

Reputation: 5818

Probably the best thing to do would be to make the button unavailable or disabled until the load is complete. Another option may be to cue the request if the load is not complete.

Something like:

private var slideLoaded:Boolean = false;
private var requestCued:Boolean = false;

imageLoader = new BulkLoader("imgLoader");
imageLoader.add(new URLRequest(paths[0]), {id:"slide_0"});                                
imageLoader.get("slide_0").addEventListener(Event.COMPLETE, onFirstSlideLoaded);
imageLoader.start(1);

fullScreenButton.addEventListener(MouseEvent.CLICK, showFull);

function showFull(e:MouseEvent):void {   
    if (slideLoaded)
        stage.addChild(mcSlideHolder);
    else 
        requestCued = true;
}

function onFirstSlideLoaded(e:Event):void {
    slideLoaded = true;
    firstImage = imageLoader.getBitmap(e.currentTarget.id);
    currentImage.addChild(firstImage);
    mcSlideHolder.addChild(currentImage);
    if (requestCued)
        stage.addChild(mcSlideHolder);
}

Upvotes: 1

Related Questions