Actionscript 3.0我试图弄清楚如何访问事件目标的属性

时间:2013-07-21 23:04:21

标签: actionscript-3 flash actionscript

我正在试图弄清楚如何引用目标的类。以下是一些代码:

xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
    xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);

    XMLItem[i] = xmlObj;
    //trace(XMLItem);
    MovieClip(root).main_mc.thumb_mc.addChild(XMLItem[i]);
    if (i <= 0) {
        XMLItem[i].x = 20;
        XMLItem[i].y = 0;
    } else if (i > 0){

        XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
        XMLItem[i].y = 0;

    }
        XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
        XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
        XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);



}

}

function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
trace(FilmVideo(e.target));
/MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.target)._APILoad, FilmVideo(e.target)._videoURL);
}

XMLItem是一个存储我自定义的类对象的数组(类名称是基于movieclip的FilmVideo)。 _thumbToMC是我的自定义类中返回movieclip的方法。该类具有存储在其属性中的信息,我想通过makeThumbClick函数中调用的函数。但是,我不知道怎么做。 e.target引用_thumbToMC movieclip而不是类。我参考课程吗?提前谢谢你:)

这是班级:

package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.URLRequest;

public class FilmVideo extends MovieClip
{
    public var _nameXML:String = "";
    public var _title:String = "";
    public var _thumbURL:URLRequest;
    public var _videoURL:URLRequest;
    public var _APILoad:String = "";

    public var loader:Loader = new Loader();

    public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
    {

        _nameXML = name;
        _title = title;
        _thumbURL = new URLRequest(thumbURL);
        _videoURL = new URLRequest(videoURL);
        _APILoad = APILoad;

        //trace(_name);
        //trace(_title);
        //trace(thumbURL);
        //trace(videoURL);
        //trace(_APILoad);

        this.addChild(loader);
        loader.load(_thumbURL); 

    }
}
}

2 个答案:

答案 0 :(得分:0)

我不是百分百肯定我理解这个问题,但假设您想要检索FilmVideo实例的属性(用户点击它似乎),那么这可能就是您要找的;

function makeThumbClick(e:MouseEvent){

    var myFilmVideo:FilmVideo = FilmVideo(e.target);

    // access properties of _thumbToMC
    myFilmVideo.randomproperty = 123;

}

答案 1 :(得分:0)

您可以将课程简化为:

package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLRequest;

public class FilmVideo extends MovieClip
{
    public var _nameXML:String = "";
    public var _title:String = "";
    public var _thumbURL:URLRequest;
    public var _videoURL:URLRequest;
    public var _APILoad:String = "";

    public var loader:Loader = new Loader();

    public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
    {

        _nameXML = name;
        _title = title;
        _thumbURL = new URLRequest(thumbURL);
        _videoURL = new URLRequest(videoURL);
        _APILoad = APILoad;

        //trace(_name);
        //trace(_title);
        //trace(thumbURL);
        //trace(videoURL);
        //trace(_APILoad);

        this.addChild(loader);
        loader.load(_thumbURL); 
    }
}
}

然后你可以将FilmVideo用作MovieClip (因为它扩展了MovieClip类)。

使用'currentTarget'而不是'target',因为它总是指向被侦听的对象,而'target'指向触发事件的对象。 more info here

xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
    xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);

    XMLItem[i] = xmlObj;
    //trace(XMLItem);
    Object(root).main_mc.thumb_mc.addChild(XMLItem[i]);
    if (i <= 0) {
        XMLItem[i].x = 20;
        XMLItem[i].y = 0;
    } else if (i > 0){

        XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
        trace(XMLItem[i].width);
        XMLItem[i].y = 0;

    }
        XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
        XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
        XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);
}

function makeThumbClick(e:MouseEvent)
{
    //var myFilmVideo:FilmVideo = FilmVideo(e.target);
    MovieClip(root).main_mc.play();
    trace(myFilmVideo._APILoad);
    MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.currentTarget)._APILoad, FilmVideo(e.currentTarget)._videoURL); 
}

如果您不想这样做

将一个引用(在类中)添加到_thumbToMC,返回它的FilmVideo对象。

_thumbMC._filmVideo = this;

然后:

function makeThumbClick(e:MouseEvent)
{
    //var myFilmVideo:FilmVideo = FilmVideo(e.target);
    MovieClip(root).main_mc.play();
    MovieClip(root).main_mc.theater_mc.videoLoader(MovieClip(e.currentTarget)._filmVideo._APILoad, MovieClip(e.currentTarget)._filmVideo._videoURL);
}