A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Passing class instance to button code created by that class...

  1. #1
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967

    Passing class instance to button code created by that class...

    Having some troubles with this. I have posted this on the actionscript forum as well and bumped it but it just sinks like a stone unanswered. Maybe you good folk would be willing/able to help?

    I will try to make this simple even though it confuses me.

    I have a class, ship. Inside there I am making some buttons and assigning them onRelease code. I want to pass the instance of the class object that made them to the buttons. So each button created would know which ship object created them.

    In my case, the buttons are cards in the hand, and the onrelease code calls a discard action. The cards have to go back to the discard pile, and the discard pile is a string in the ship objects (I have made each ship the physical manifestation of the player. All player data is held in their playing piece).

    I solved the problem by passing the value off to a _root level variable for temporary storage, which works fine for this solution, but it would NOT work if there were multiple ship objects generating cards that existed at the same time.

    To help, here is some pseudocode to show you what I am talking about. This is how I did it, is there a better way to pass the SHIPS 'this' value, and not the 'this' value of the button.

    PHP Code:
    class Ship extends MovieClip {
    .
    .
    .
    public function 
    makebutton() = {
       var 
    mybutton _root.attachMovie(//valid object from lib at valid depth);
       //code to setup/place movie
       
    _root.tempshipinstance this;
       
    mybutton.onRelease = function() {
          
    trace(_root.tempshipinstance " created me!");
          
    trace("I am " this);
       }
    }

    The created button does not refer back to what made it in this example, but whatever the value of _root.tempshipinstance is when that button is clicked. For my discard use, it works fine. Only one local ship is discarding at once. But if I ever needed to do this for buttons from different ship that would exist at the same time I would not know how to pass the ship instance to the function.

    Is there a way to do this? Any variables inside that onRelease function definition are local to the onRelease function and won't evaluate until it is activated it seems. I have tried all sorts of convolutions of _root[this], eval(this), and this[this] and 'this' always comes out to be the button instance.

    Can this be done?

  2. #2
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306
    Not sure I understand 100% but what I think your trying to do is this.

    PHP Code:
        public function makeButton (mc:MovieClipstring:String) {
            
            var 
    myButton:MovieClip _root.attachMovie(stringstring_root.getNextHighestDepth())
                                                                                                  
            
    myButton.mc mc
            
            myButton
    .onRelease = function (){
                
                
    trace (this.mc " created me")
                
    trace ("I am " this)
                
            }
            
        } 
    Last edited by everfornever; 08-31-2007 at 10:52 AM.

  3. #3
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Ah, that is way too easy!

    myButton.mc = mc //OUTSIDE THE FUNCTION!!! GAH!

    DUH!
    Thank 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