2013-03-06 77 views
1

我想写适用TextFormats他们属于TextField的功能应用的格式。 问题是,我没有管理它进入正确的“表格”(如果甚至有一个正确的形式)。AS3通过功能

我的功能看起来像这样的时刻

function applyFormat(TextFild:String,Format:String,EmbFont:String,NameInJson:String){ 

// 
//example:applyFormat("myTextField","myTextFormat","myFont","TextStuff") 
// 
//myData=json object 
//all textfields etc. already exist. Just anyone wants to ask if they've been already generated. 

//at first i thought it could be as easy as this 
this[Format].font=myData.NameInJson.Font //doesn't work obviously 

//then I tried this method 
this[Format].font=myData.this[NameInJson].Font //doesn't work either 

//couldn't find a real solution for this on the internet...  
//how it should turn out in the end: myTextFormat.font=myData.TextStuff.Font 
} 

的myData的相关部分看起来是这样的:

"TextStuff": 
{ 
"Font":"Arial", 
"Size" : "16", 
"Bold" : "true", 
"Color" : "0xFFFFFF" 
} 

,我需要改变我的JSON文件,使这个可能吗? 我忽略了一种处理这种变量/值的方法吗?

回答

1

this[Format]将查找的名称格式的类的属性。 AFAIK课程必须充满活力才能发挥作用。

如果你把你TextFormat情况下,在该类的对象时,它会更容易管理:

// this is a property in the class 
private var _textFormats:Object = {}; 

// create and store text formats in the object 
var textFormat:TextFormat = new TextFormat(); 
_textFormats["myTextFormat"] = textFormat; 

现在,假设你得到一个JSON字符串是这样的:

{ 
"TextStuff":{ 
    "Font":"Arial", 
    "Size" : "16", 
    "Bold" : "true", 
    "Color" : "0xFFFFFF" 
    } 
} 

以下应该与这个电话联系applyFormat("myTextField","myTextFormat","myFont","TextStuff")

function applyFormat(TextField:String, Format:String, EmbFont:String, NameInJson:String):void 
{ 
    var myData:Object = JSON.parse(theJSonString); 
    _textFormats[Format].font = myData[NameInJson].Font; 
} 
+0

我打我的额头,当我哇阅读这个解决方案。 它就这么简单!为什么我没有想到这个?我觉得这么愚蠢现在... 无论如何,非常感谢您的解决方案,它帮助了我很多!(会给予好评,如果我有足够的声誉...) – 2013-03-07 09:40:03

+0

不客气:) – 2013-03-07 13:52:01