Copy link to clipboard
Copied
If I have a public var in my document class that I want to change the value of when a button is clicked, sorta like a global. How do I go about doing that... ?the right way?.
My document class extends MovieClip, my button class extends fl.controls.button.
I've got it working currently using a hack line of code:
MovieClip(root).myVar = someValue
I don't like it though... very "AS2ish".
Copy link to clipboard
Copied
i don't think you need to reference the root unless you're placing code off the main timeline.
Copy link to clipboard
Copied
When you make the button you could pass in a reference to the document class. LIke:
var myfancybutton:FancyButton = new FancyButton(this);
and in the fancy button constructor:
private var docClass:MovieClip;
public function FancyButton(docMC:MovieClip){
docClass = docMC;
}
then later:
private function somethingClicked(event:MouseEvent){
docClass.aVar = aValue;
}
Or something on those lines.
Copy link to clipboard
Copied
if the button's not placed on the main timeline, that won't work - the variable will be out of scope of the movieclip's timeline. he'll still need to reference the root property of the passed movieclip and because that's accessible via the keyword "this" (or implicit in the code he's currently using) in the button class, there's no reason to pass that movieclip.
Copy link to clipboard
Copied
Exactly... that's what makes my nose wrinkle. What if the button is nested a few levels down in another component or other MovieClip?
Copy link to clipboard
Copied
In my example line:
var myfancybutton:FancyButton = new FancyButton(this);
that was supposed to be in the document class, so the 'this' would point to the document class, which he said extends movieclip, and where his variables were living.
If the button was created elsewhere, and the button still needed to get to the maintimeline without using MovieCip(root), the thing that created the button would pass on its reference to the maintimeline:
var myfancybutton:FancyButton = new FancyButton(maintimelineVar);
Would that not work?
Copy link to clipboard
Copied
no. you'd have to reference the root property of the passed movieclip and you'd have to type that root property as a movieclip. and that's no benefit over what's currently being done.
i don't believe there's any significant improvement that can be made to the original coding other than what i posted in my first message. the op could change his code and do this a different way including your way or making myVar a pseudo-global variable but neither of those will be an improvement.
Copy link to clipboard
Copied
I think I'll stick with what I've been doing. I do like the idea of making a psudo-global. Others have done similar things where they make a "global" class with static variables.
I do appreciate the discussion and thank you both for your points of view.
Copy link to clipboard
Copied
if that's what you want to do, you can use:
package {
public class glo {
public static var bal:Object=new Object();
}
}
import glo wherever you want to use it and assign:
glo.bal.myVar=whatever;
or retrieve:
glo.bal.myVar;
Copy link to clipboard
Copied
Nice! I like it! Thanks!
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
Hm. Ok... that's a good point. But let's go super simple here... say the button already exists on the stage. I'm not going to make one dynamically.
I guess could still create a reference to the doc class in my custom button class by doing something like:
package {
import fl.controls.Button
public class FancyButton extends Button {
private var docClass:MovieClip;
public function FancyButton() {
docClass = this.parent
}
}
}
That still doesn't seem right to me though. I mean, it'll work but it makes me do that nose wrinkle thing...