How can I use Event class from Starling and Flash both in the same class

244 Views Asked by At

While working with Starling, I am encountering this problem. It's important to use the flash Event sometimes.

import starling.events.Event;
import flash.events.Event ;
var _myFile_Fr:FileReference = new FileReference();
_myFile_Fr.addEventListener(Event.COMPLETE, onFileLoaded);
_myFile_Fr.load();
function onFileLoaded(event:Event ):void
{
  trace( event.target.data ) // <<<< compiler error:       


}

Access of possibly undefined property data through a reference with static type starling.events:EventDispatcher

I tried to use the full specification

function someEventHandler(event:flash.events.Event ):void
{
  ....
}

Also this way :

_myFile_Fr.addEventListener(flash.events.Event.COMPLETE, onFileLoaded);
_myFile_Fr.load();

function onFileLoaded(e:Event ):void 
{
    trace( e.target.data );
}       

But the above doesnot work, and shows compile time error.

access of undefined property Flash

Then what's the way out ?

Access of possibly undefined property data through a reference with static type starling.events:EventDispatcher. With flash.events.Event, it works fine.

4

There are 4 best solutions below

5
payam_sbr On

from starlling Doc

Event objects are passed as parameters to event listeners when an event occurs. This is Starling's version of the Flash Event class.

this class do all that flash.events.Event did before with its additional options. what would be the advantage of using import flash.events.Event in your case?

however

addEventListener(flash.events.Event.ADDED, handler);
function handler(e:flash.events.Event) {}

does not cause any error in regular way.

Also from starlling Doc

In that case, you can subclass "Event" and add properties with all the information you require.

0
Vishwas On

The only way I found was not to use Event class name anywhere. So, what worked for me is this :

import starling.events.Event;

var _myFile_Fr:FileReference = new FileReference();
_myFile_Fr.addEventListener(Event.COMPLETE, onFileLoaded);
_myFile_Fr.load();
function onFileLoaded(event):void //<< Don't use class name specification
{
  trace( event.target.data )  


} 
0
Josh Tynjala On

access of undefined property Flash

You are correct that you need to use the fully-qualified class name flash.events.Event, but I suspect that you made a typo. Notice that the error says Flash where the first letter is upper-case. ActionScript is case sensitive so Flash.events.Event will not work. It must be flash.events.Event where flash is all lower-case.

import starling.events.Event;
import flash.events.Event;
var _myFile_Fr:FileReference = new FileReference();
_myFile_Fr.addEventListener(flash.events.Event.COMPLETE, onFileLoaded);
_myFile_Fr.load();
function onFileLoaded(event:flash.events.Event):void
{
    //see below for changes
}

Access of possibly undefined property data through a reference with static type starling.events:EventDispatcher

This means exactly what it says. The target property of an event is typed as EventDispatcher. If you know that it's something more specific, you need to cast it, like this:

var file:FileReference = FileReference(event.target);
trace( file.data );
0
loganjones16 On

Problem is caused because the compiler thinks the event you want is a starling.events.Event. Starling events have an attribute target but it (unlike flash events) is of the specific type EventDispatcher where flash is just a generic Object. So when you look for event.target.data you are trying to look for the attribute data on a starling EventDispatcher object. There is no data attribute in the EventDispatcher class, so it is throwing that error.
You need to be careful that when you have both starling.events.Event and flash.events.Event, you will need to specify exactly which one you are expecting (using its fully qualified name).

public function onLoad(event:flash.events.Event):void

or

public function onLoad(event:starling.events.Event):void

but not

public function onLoad(event:Event):void

unless you only imported either flash or starling events, but not both.