To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Games

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-31-2007, 10:59 AM   #1
Alluvian
Senior Member
 
Alluvian's Avatar
 
Join Date: Jun 2006
Posts: 873
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?
Alluvian is offline   Reply With Quote
Old 08-31-2007, 11:21 AM   #2
everfornever
Truimagz.com
 
everfornever's Avatar
 
Join Date: Sep 2006
Location: St. Louis
Posts: 1,305
Not sure I understand 100% but what I think your trying to do is this.

PHP Code:
    public function makeButton (mc:MovieClip, string:String) {
        
        var
myButton:MovieClip = _root.attachMovie(string, string, _root.getNextHighestDepth())
                                                                                              
        
myButton.mc = mc
        
        myButton
.onRelease = function (){
            
            
trace (this.mc + " created me")
            
trace ("I am " + this)
            
        }
        
    }
__________________
http://www.truimagz.com

Last edited by everfornever; 08-31-2007 at 11:52 AM.
everfornever is offline   Reply With Quote
Old 08-31-2007, 11:44 AM   #3
Alluvian
Senior Member
 
Alluvian's Avatar
 
Join Date: Jun 2006
Posts: 873
Ah, that is way too easy!

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

DUH!
Thank you!
Alluvian is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Games

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:45 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.