您可以创建两个类来管理主题:
第一个管理主题名称
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;
}
}
}
第二个管理资产
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();
});
}
}
}
在您的 Flash 库中,您需要为每个符号设置链接名称,如下所示:
myButtonDark
myButtonLight
mySpriteDark
mySpriteLight
现在您必须设置当前样式名称
Styles.setStyles(Styles.LIGHT);
最后,您可以创建所需的实例
// 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")) );
我希望它对你有用:)