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?