Ha ha, this is why we love Flash so much, and why it makes us want to put a bucket on our head, and have our children whack the bucket with sticks.

So here's the thing. Your class has a method called doStuff(), and a button mc called button_mc. You want it to fire doStuff() when you click button_mc. Here is how I do it:

In your class file:

button_mc.owner = this;         
// the mc now has a new property 'owner' which is a reference to the class

button_mc.onRelease = function() {
        this.owner.doStuff();
}

This refers to the movieclip, owner refers to the class.

Using UI components is a little better but not much:

In the class file:

button_btn.addEventListener( "click", Delegate.create( this, doStuff ) );

Annoying but it works. Enjoy!

OK
DAH

PS If anyone has a better way to do this, I am SO all ears.

On Feb 4, 2007, at 3:56 PM, Jason Boyd wrote:

OK this is making me nuts. It seems to be an impossible scope problem,
to do something that ought to be very very possible, and in fact
common. The basic problem: I want to have initialization code in a
Main class that hooks up GUI controls to methods in this class (acting
as a controller class essentially). For various reasons, I do not want
to use components; the GUI elements are either Buttons or MovieClips.
Again, this ought to be possible, right?

So here's the problem case:

class Main {
 var myButton:Button; // placed on stage in authoring time

 function init() {
   myButton.onRelease = function() {
     doSomething();
   }
 }

 function doSomething() {
   trace("something");
 }
}

This does not work, nor is there any way I can wrap my head around
getting it to work, as the anonymous function assigned to the button
event handler has "this" scoped as the button itself, so
"doSomething()" refers to a non-existent function
myButton.doSomething(). Simply assigning the member function to the
button event handler directly doesn't help, as this function is then
unable to call other member functions for the same reason. E.g.:

function init() {
 myButton.onRelease = doSomething();
}

function doSomething() {
 // this will be called now, but "this" still refers to the button,
not objects of this class
}

There has *got* to be some way around this issue. Right?
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

--
David Ham
http://anthropomorphy.org               ::      +1 630 297 1273
http://davidham.com                     ::      [EMAIL PROTECTED]



_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to