Skip to main content
February 5, 2009
Question

class scope issue?

  • February 5, 2009
  • 9 replies
  • 463 views
I am trying to write a class that activates my buttons. I can't seem to make the onRollOver call a function. Why is that? What am I doing wrong?

Thank you for any help with this!



class HomeMain extends MovieClip
{
//Button Variables
private var home_btn0:MovieClip;
private var home_btn1:MovieClip;
private var numOfBtns:Number = 2;
private var whichOne:Number;

public function HomeMain(Void)
{
activateBtns();
}

private function activateBtns():Void
{
for(var i:Number = 0; i < numOfBtns; i++)
{
var ref:MovieClip = this["home_btn" + i];
ref.id = i;
ref.onRollOver = function():Void {
rollOverActions(this.id);
}
}
}

private function rollOverActions(whichOne):Void
{
trace("over");
}
}
This topic has been closed for replies.

9 replies

kglad
Community Expert
Community Expert
February 6, 2009
that should only make a difference if you were losing scope when the function that contains that code is called and i don't see that. but as long as it works, leave well enough alone.
February 6, 2009
No, here is what I have now:
for(var i:Number = 0; i < numOfBtns; i++)
{
var ref:MovieClip = this["home_btn" + i];
ref.id = i;
var hm = this;
//Btn actions
ref.onRollOver = function():Void {
hm.rollOver2(this.id);
}

}
kglad
Community Expert
Community Expert
February 6, 2009
i didn't think that would work because i thought you'd defined your buttons on your fla's timeline.

so, are you using:

var ref:MovieClip = hm["home_btn" + i];
February 6, 2009
Yes, that seems to work! Is that what I should have done?

Thank you so much for the help!
kglad
Community Expert
Community Expert
February 6, 2009
is that working for you?
February 5, 2009
I think I figured it out:

var hm = this;
ref.onRollOver = function():Void {
hm.rollOverActions(this.id);
}

February 5, 2009
so if I do this, it works:
_root.home_mc.rollOutActions();

but I really don't want to use _root. How can I avoid that? I also tried:
_parent.home_mc.rollOutActions();
but that does not work either, besides the point, that that would be hardly better than using _root.


February 5, 2009
Hi Kglad,
thank you for the help!

I am sorry, but I quite understand what you are telling me?
kglad
Community Expert
Community Expert
February 5, 2009
"this" is your HomeMain class when used in the code you showed and you probably want "this" to reference the timeline where your buttons are defined.

to remedy, pass a timeline reference to your constructor and use that reference instead of "this" in activateBtns().