Extending Loader
-
03-06-2021 - |
题
I´m trying to extend the Loader class.
I want to store a variable on it.
Example:
package
{
import flash.display.Loader;
public class MyLoader extends Loader
{
private var _typeOfGallery:String
public function MyLoader()
{
super()
}
public function set typeOfGallery(value:String):void
{
_typeOfGallery = value
}
public function get typeOfGallery():String
{
return _typeOfGallery
}
}
}
Then I´m using like this:
var loader:MyLoader = new MyLoader()
loader.typeOfGallery = 'games'
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded)
function loaded(e:Event):void{
trace(e.target.typeOfGallery)
}
I´m receiving this error:
ReferenceError: Error #1069: Property typeOfGallery not found in flash.display.LoaderInfo and has no pattern value.
(I have translated from portuguese to english the error message)
How could I extend Loader so I don´t receive this error?
Thanks.
解决方案
Loader
does not dispatch the Event.COMPLETE
event. You should listen to
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded)
and then get your MyLoader
with
function loaded(e:Event):void{
trace(((e.currentTarget as LoaderInfo).loader as MyLoader).typeOfGallery);
}
In your code, e.target
is a LoaderInfo
because the COMPLETE
event bubbles to the Loader
itself.
其他提示
loader.contentLoaderInfo.typeOfGallery = 'games'
IS WRONG - you want to go for the loader not for its contentLoaderInfo...
loader.typeOfGallery = 'games'
trace(e.target.typeOfGallery) should work cause its a reference to the loader!!
不隶属于 StackOverflow