Skip to main content
PhilBailey
Participant
August 21, 2007
Question

Global Access to Listener Function

  • August 21, 2007
  • 1 reply
  • 270 views
Hello All --

I am using a third-party Flash add-on tool that will let me get a device's battery level (among other things) via listeners. The following code is in my first actionscript frame:

-------------------------------------------------------------
import ssp.device.*;
var statusListener:Object = new Object();

// set-up status listener to get battery level
statusListener.getBatteryLevel = function(e)
{
_global.batteryLevel = e;
}

Status.addEventListener(statusListener);
Status.getBatteryLevel();
-------------------------------------------------------------

The final line in the preceding script successfully sets _global.batteryLevel. The problem is that the script is run only once, when the application starts. What I need to be able to do is to call Status.getBatteryLevel() from a battery monitoring movieclip within my main movie. The movieclip runs constantly to update a display of remaining battery charge.

Unfortunately, whenever I try to execute Status.getBatteryLevel() from any frame other than the one where the listener is set-up, the function does not work. Is there something that I need to do to make the function call available globally?


Regards,


-- Phil
This topic has been closed for replies.

1 reply

Inspiring
August 21, 2007
It seems unusual that the Status object only dispatches events when you request them... but perhaps its just a model I'm not used to. Anyhow...

Where is your monitoring clip relative to the timeline that holds Status?

If its also in the same timeline then you should be able to get to Status from its timeline by using

_parent.Status.getBatteryLevel();

I never really use the _global object, but I guess you could do that too.
Status.addEventListener(statusListener);
Status.getBatteryLevel();
_global.getBatteryLevel = Status.getBatteryLevel;

then just use
_global.getBatteryLevel();

I think that would work.



PhilBailey
Participant
August 21, 2007
Hi GWD --

Using the _parent. prefix probably wouldn't work since I have tried the function call right from another frame in the same level. Fortunately, your suggestion to use the _global scope worked great! Thanks very much!


-- Phil