A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: How to access a class instance property

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    21

    How to access a class instance property

    I have a problem with the piece of code that's been annoying me for a week. It should be very simple but all my attempts at a solution somehow seem to get thwarted by an invisible hand that wants me to fail.

    Here is the scenario: I am trying to create an image strip consisting of image thumbnails. When one mousovers each image it should print its information (say a name) in a textfield.

    I created an image container class that loads the images through a url request, then an image strip class that receives an array with image names and urls and displays them in a row. So far so good.

    However when it comes to showing the info I have no clue how to address which image is being pointed at. I put a variable called imageIndex inside my imageContainer object, then put all the created containers in imageStrip's for-loop inside an array called imgContainerArray. I created a for loop in my fla that goes through this array and assigns all those containers a mouseover event listener. What I'm stumped with is that there is no way I can access the imageIndex variable from my function to tell the code which image is currently being mouseovered. It keeps giving me an error.

    Here is the code:

    Code:
    for(var i:Number=0; i<myImageStrip.imgContainerArray.length;i++){
    	
    myImageStrip.imgContainerArray[i].addEventListener(MouseEvent.MOUSE_OVER, 
    onMouseOverImg, false, 0, true);
    	
    trace(myImageStrip.imgContainerArray[i].imageIndex); //this works and 
    shows me a list of all my indexes
    }
    
    function onMouseOverImg(evt:MouseEvent){
    
    	trace(evt.target.imageIndex); //this doesn't and is what I need to work
    }
    Is it something about evt.targe and eventlistener functions I don't know. Please help, I'm on the very last leg of a project and this is the only thing holding me back. Thanks.

  2. #2
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    It really helps to know the error. I'm guessing it's something like property X not found on blah blah.

    I've used similar setups and it seems to work fine using the target or currentTarget. You may have to coerce the event.target as the class you're trying to target.

    e.g.
    PHP Code:

    trace
    ImageClass(evt.currentTarget).imageIndex ); 
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  3. #3
    Junior Member
    Join Date
    Nov 2006
    Posts
    21
    Wow, you actually solved it. The issue was the use of target instead of currentTarget. Once I replaced it, it worked. Thank you.

    Corrected code:

    Code:
    function onMouseOverImg(evt:MouseEvent){
    
    	trace(evt.currentTarget.imageIndex); 
    }
    Oh syntax! *Shakes head*

  4. #4
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center