Skip to main content
Participant
April 24, 2012
Question

Disposing of stagewebview

  • April 24, 2012
  • 6 replies
  • 4672 views

Hi guys

I have a problem when trying to dispose of a stagewebview object.  I would like to be able to trigger events for disposing of stagewebview from outside of the function the stagewebview was invoked.

Example:

function playIt(event:MouseEvent){

   

    var nowPlaying = new mcPlaying();

    nowPlaying.x = 10;

    nowPlaying.y = 416;

    nowPlaying.name = "nowPlaying";

     

    stage.addChild(nowPlaying);

       

    var idPath = "idPathVars;

   

    var webView:StageWebView = new StageWebView();

     

      webView.stage = this.stage;

      webView.viewPort = new Rectangle(10, 440, 300, 70);

      webView.loadURL("http://myURL.com/Free.html?id=" + idPath);

         

    nowPlaying.btnCloseTrack.addEventListener(MouseEvent.CLICK, disposeTrack);

   

    function disposeTrack(event:MouseEvent){

        webView.dispose();

       

            stage.removeChild(nowPlaying);

           

    }

   

   

}

Currently I am disposing of the stageWebView object from within scope, but what I would like to do is call dispose from outside the parent function.

I have tried invoking the stageWebView from above the function but I could not get that going, any help would be greatly appreciated.

Thank you

This topic has been closed for replies.

6 replies

Inspiring
May 1, 2012

//hides it

webView.stage = null;

//shows it

webView.stage = this.stage;

//gets rid of it

webView.dispose();

Calling dispose() is optional. If you do not maintain a reference to this StageWebView instance it will be eligible for garbage collection. Calling dispose() can make garbage collection occur sooner, or at a more convenient time.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/StageWebView.html#dispose()

//--

my guess is that if you use dispose, you will need to recreate your webView:

//here is where the error occurs and where I need to set prerequiste parameters

      webView = new StageWebView();

      webView.viewPort = new Rectangle(10, 440, 300, 70);

      webView.stage = this.stage;

April 30, 2012

Just for clarification, are you creating the webView variable as a class variable or a local variable?  I only ask because I don't see the webView variable creation code above.

For example, as a class variable:

private var _webView:StageWebView = new StageWebView();

private function doStuff():void

{

     _webView.doStuff();

}

Or are you creating it this way...

private function doStuff():void

{

     var webView:StageWebView = new StageWebView();

}

Let me know.

Best,

cb

Participant
April 30, 2012

The instance of the StageWebView object is created above the functions in scope to the preceeding functions

like so:

private var _webView:StageWebView = new StageWebView();

private function doStuff():void
{
     _webView.doStuff();

     //Code inits viewport,stage + loadURL
}

private function dispose():void
{
    

            webView.viewPort = null;

            webView.stage = null;

            webView.dispose();

           

            var child:DisplayObject = stage.getChildByName("btnPortable");

            if (child) {

                trace("removing btnPortable");

                stage.removeChild(child);

            }

           

            var child2:DisplayObject = stage.getChildByName("nowPlaying");

            if (child2) {

                trace("removing nowPlaying");

                stage.removeChild(child2);

            }


}

Inspiring
April 24, 2012

one approach could be declaring your webView variable outside of your function so it's accessible from anywhere, instead of from inside the function only. the other thing you could do, is keep your functions from being nested, for the same reason:

var webView:StageWebView = new StageWebView();

function playIt(event:MouseEvent){

//your code...

}

function disposeTrack(event:MouseEvent){

//your code...

}

April 30, 2012

While I believe iFlashAppsToo anwered your question above, here is some more detailed code:

private var _webView:StageWebView = new StageWebView();

private function addWebView():void

{

      _webView.viewPort = new Rectangle(10, 440, 300, 70);

      _webView.stage = this.stage;

      _webView.loadURL("URL-to-load");

    

     btn.addEventListener(MouseEvent.CLICK, closeWebView);

}

private function closeWebView(e:MouseEvent):void

{

     _webView.stage = null;

     _webView.dispose();

}

Hope that helps.

Best,

CB

Participant
April 30, 2012

Hi thanks for your suggestion and yes the solution is similar to what iFlashApps, however I wish to call closeWebView from a different event like so:

function disposeTrack(event:MouseEvent){

        if(webView.stage != null){

            webView.viewPort = null;

            webView.stage = null;

            webView.dispose();

           

            var child:DisplayObject = stage.getChildByName("btnPortable");

            if (child) {

                trace("removing btnPortable");

                stage.removeChild(child);

            }

           

            var child2:DisplayObject = stage.getChildByName("nowPlaying");

            if (child2) {

                trace("removing nowPlaying");

                stage.removeChild(child2);

            }

        }

    }

The above code is called prior to the below and works as wished, however, the code below throws an error -

ArgumentError: Error #2004: One of the parameters is invalid.

    at flash.media::StageWebView/set viewPort()

I think viewPort has other properties/parameters that need to be set before assigning a new Rectangle, maybe width and height or new ViewPort.  I will endevour to track down what the parameters that are required.  Below is where the error occurs and I should say the first time it is run it is fine and the error occcurs in playPlaylist function and not the above, disposeTrack();

function playPlaylist(event:MouseEvent){

   

    var nowPlaying = new mcPlaying();

    nowPlaying.x = 10;

    nowPlaying.y = 416;

    nowPlaying.name = "nowPlaying";

     

    stage.addChild(nowPlaying);

   

    var btnPortable = new mcPortable();

    btnPortable.x = 10;

    btnPortable.y = 416;

    btnPortable.name = "btnPortable";

    stage.addChild(btnPortable);

   

    btnPortable.addEventListener(MouseEvent.CLICK, portableMode);

   

    var arrayPoint = event.currentTarget.parent.name - 1;

    trace("array point "+arrayPoint);

   

    var allPermalinks = event.currentTarget.parent.parent.parent.permalinkOrder.text;

    var permalinkArray = allPermalinks.split(",");

   

    var trackPath = "";

   

    for(i=arrayPoint; i<permalinkArray.length; i++){

        trace("incremental i = "+i);

        trackPath += permalinkArray+",";

    }

   

     //here is where the error occurs and where I need to set prerequiste parameters

      webView.viewPort = new Rectangle(10, 440, 300, 70);

      webView.stage = this.stage;

     

      webView.loadURL("myURL/Free.html?track=" + trackPath);

     

   

    nowPlaying.btnCloseTrack.addEventListener(MouseEvent.CLICK, disposeTrack);

   

}

ArgumentError: Error #2004: One of the parameters is invalid.

    at flash.media::StageWebView/set viewPort()

Any help with this would be greatly appreciated