2

I'm trying to figure out how to reference the class of a target. Here is some of the code:

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);
}

The XMLItem is an array that's storing a class object I custom made (the class name is FilmVideo based off movieclip). The _thumbToMC is a method within my custom class that returns a movieclip. The class has info stored within its properties I would like to pass through a function called in the makeThumbClick function. However, I have no idea how. e.target reference the _thumbToMC movieclip rather than the class. I do I reference the class? Thank you in advance :)

Here is the class:

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); 

    }
}
}
plasmacel
  • 8,183
  • 7
  • 53
  • 101
  • I'm not sure what your exact problem is, but as a start this may help: `_thumbToMC` references your *method*, but doesn't actually call it, if you want to reference the `MovieClip` it returns you need to use `_thumbToMC()` – David Mear Jul 21 '13 at 23:17
  • @DavidMear I suspect you are right, but it is possible _thumbToMC maybe be a getter method that returns a MovieClip instance. – Kaushal De Silva Jul 21 '13 at 23:20
  • Kaushal De Silva, that is correct. I edited the post with my class. – Darnell E. Evans Jul 21 '13 at 23:41

2 Answers2

0

I'm not 100% sure I understand the question, but assuming you want to retrieve the properties of the FilmVideo instance (which it appears the user is clicking on) then maybe this is what you are looking for;

function makeThumbClick(e:MouseEvent){

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

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

}
  • I can't do FilmVideo(e.target) because as you can see in the class file, it's expecting 5 parameters (in which are all xml data). :) – Darnell E. Evans Jul 21 '13 at 23:42
  • @DarnellE.Evans that line is not instantiating a new FlashVideo object (it is not calling the constructor so the number of arguments is not relevant) - it is casting. You can use that line. I guarantee it with my life :) – Kaushal De Silva Jul 22 '13 at 00:13
  • It makes sense now, since it's not a new instance of FilmVideo correct? I'm still an infant when it comes to classes. :) – Darnell E. Evans Jul 22 '13 at 00:26
  • Hmm, I getting the same error with this code as I am with plasmacel code. Error #1034: Type Coercion failed: cannot convert flash.display::Loader@2c300031 to filmvideo.FilmVideo. at filmvideo_fla::MainTimeline/makeThumbClick(). I know it can't be an issue with the loader since the thumbnails are indeed showing up when I run the flash file – Darnell E. Evans Jul 22 '13 at 00:38
  • it looks like your code is now based on plasmacel's ... I am guessing you need to change your coercion to: FilmVideo(Loader(e.target).parent); – Kaushal De Silva Jul 22 '13 at 01:00
  • I was using both yours and his code lol. Thank you for all your help though, we figured it out. Had to use currentTarget rather than target! :) – Darnell E. Evans Jul 22 '13 at 01:12
0

You could simplify your class to:

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); 
    }
}
}

Then you can use FilmVideo as a MovieClip (since it extends the MovieClip class).

And use 'currentTarget' instead of 'target', because it always points to the listened object, while 'target' points to the object which fired the event. 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); 
}

If you don't want to do this way

Add a reference (inside the class) to the _thumbToMC back to it's FilmVideo object.

_thumbMC._filmVideo = this;

Then:

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);
}
Community
  • 1
  • 1
plasmacel
  • 8,183
  • 7
  • 53
  • 101
  • Wow, thank you for simplifying my class. That makes things so much better. Plus, it just makes too much sense lol. I put in the code: videoLoader(FilmVideo(e.target)._thumbURL, FilmVideo(e.target)._APILoad); and now I'm getting an (Type Coercion failed: cannot convert flash.display::Loader@3157d031 to filmvideo.FilmVideo.) error. I also trace FilmVideo(e.target)._APILoad, same thing – Darnell E. Evans Jul 22 '13 at 00:17
  • Alright, I updated the code in the original post with what I am using now. :) – Darnell E. Evans Jul 22 '13 at 00:41
  • You don't have a _thumbToMC object anymore in the FilmVideo instances. Use the FilmVideo object itself as you used _thumToMCs, since FilmVideo is a MovieClip too. See my edit. – plasmacel Jul 22 '13 at 00:47
  • Yes, that is correct. I used your first example for the class and got rid of the thumbToMC method. So are you saying in your first example, I still needed that method because the thumbnails are still showing up without it – Darnell E. Evans Jul 22 '13 at 00:51
  • Sorry, I have edited wrong part of the code. :D That belongs to the first example. Take a look now. – plasmacel Jul 22 '13 at 00:55
  • 1
    SUCCESS!!!! currentTarget was it! THANK YOU SO MUCH PLASMACEL! That helped me out tremendously, and learned a few new things in the process :D – Darnell E. Evans Jul 22 '13 at 01:10