Reputation: 21
I inherited a project consisting of a flash movie with graphic components and an .as file with the code. i want to alter the appearance of various graphic elements.
example:
if (skin="client1")
-> all buttons have to be blue, if (skin="client2")
-> all buttons have to be yellow - and so on.
I am stuck with a button:
I tried and nested a movie with different colors (frame1=blue
, frame5=yellow
) inside the button, but i can't find a way to goToAndStop
that movie from the .as file.
I tried to check a variable from inside that movie (if skin="client1" goto frame "blue"
), but I can't seem to get access to the other variables, which are defined inside the .as file.
Upvotes: 2
Views: 441
Reputation: 18757
Since the project is yours, you might provide an additional method to your button to accept a certain change, say, if your nested movie clip has several frames with labels, and you have them arranged for colors like 'blue', 'green', 'black' and so on, you can create a set of skin constants of type String, like Alexander Ruiz suggested, which can also serve as labels for your MCs to switch displayed frame. So, you make a function inside your button class, which can reach your nested movie clip even if it is private, like this:
public function AcceptSkin(skin:String):void {
try {
nestedMC.gotoAndStop(skin);
}
catch(e:Error) {
trace('Error applying style',skin,e.toString());
}
}
You can do more in such a method.
Now, when you want all of your buttons go yellow, and you have a "yellow" labeled frame in your MCs (which supposedly contains yellowish appearance for that button), you call theButton.AcceptSkin('yellow');
for each of the buttons. But if you are using SimpleButton class for your buttons' base class, I'd advise you making your own class for buttons, because SimpleButton class has no easy method of receiving its contents.
Upvotes: 0
Reputation: 363
You can create a two classes to manage the themes:
The first manages the theme name
package
{
public class Styles
{
public static const DARK:String = "dark";
public static const LIGHT:String = "light";
public static var currentStyleName:String = "dark";
public static function setStyles(styleName:String):void
{
currentStyleName = styleName;
}
}
}
The second one manages the assets
package
{
import flash.events.EventDispatcher;
import flash.display.*;
import flash.utils.getDefinitionByName;
import Styles;
public class Assets extends EventDispatcher
{
public static function setStyles(styleName:String):void
{
currentStyleName = styleName;
}
public static function getClass(name:String):Class
{
var TheClass:Class = getDefinitionByName(name) as Class;
return TheClass;
}
public static function sprite(name:String):Sprite
{
return new (getClass(name)) as Sprite;
}
public static function simpleButton(name:String):SimpleButton
{
return new (getClass(name)) as SimpleButton;
}
public static function styledName(name:String):String
{
return name + Styles.currentStyleName.replace(/^\w/, function(firstChar) {
return firstChar.toUpperCase();
});
}
}
}
In your Flash Library you need set the linkage name for each symbol something like this:
myButtonDark
myButtonLight
mySpriteDark
mySpriteLight
Now you must set the current style name
Styles.setStyles(Styles.LIGHT);
Finally you can create the instances that you need
// All the instances created here belong to the Light theme
addChild( Assets.simpleButton(Assets.styledName("myButton")) );
addChild( Assets.sprite(Assets.styledName("mySprite")) );
// Changes the theme to dark
Styles.setStyles(Styles.DARK);
// All the instances created here belong to the Dark theme
addChild( Assets.simpleButton(Assets.styledName("myButton")) );
addChild( Assets.sprite(Assets.styledName("mySprite")) );
I hope that it works for you :)
Upvotes: 1
Reputation: 7501
AS3 is fully Object-Oriented.
If those variable are private or protected you CAN'T access them.
Upvotes: 1