尝试的TextFormat传递到其他类别,得到错误
-
20-09-2019 - |
题
快乐大家星期二:)
我发现我又用同样的字体,在上我的几个子类的,所以我想我只是做一个的字体类的处理所有这些。
反正我抓我的头围绕如何得到我的字体类的创建者TextFormats到我的其他类干净。我不认为我这样做是正确的做法,但我目前收到此错误信息:
footFont =空 类型错误:错误#2007:参数格式必须为非空
。我要的avant97的TextFormat传进我的框架类样式页脚文本
下面是我的字体类别。
package src.model
{
import flash.text.*;
public class Fonts
{
public static var data:Object = {};
public static var avant97 = new TextFormat(); // Footer copy
public static var avantFF = new TextFormat(); // Navigation Copy
public static var avant0s = new TextFormat(); // Thumbnail Titles
avant97.font = (new AvantGrande() as Font).fontName;
avant97.size = 16;
avant97.color = 0x979797;
avant97.align = TextFormatAlign.CENTER;
avantFF.font = (new AvantGrande() as Font).fontName;
avantFF.size = 16;
avantFF.color = 0xFFFFFF;
avantFF.align = TextFormatAlign.CENTER;
avant00.font = (new AvantGrande() as Font).fontName;
avant00.size = 16;
avant00.bold = true;
avant00.color = 0x000000;
avant00.align = TextFormatAlign.LEFT;
}
}
这里就是我试图avant97附加到TextField我Frame类:
package src.display{
import flash.text.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.events.Event;
// ☼ --- Imported Classes
import src.events.CustomEvent;
import src.model.Fonts;
public class Frame extends Sprite {
private var footer:Sprite = new Sprite();
private var fnt:Fonts; // <- var for Fonts Class
private var footFont:TextFormat; // var to hold avant97
// ☼ --- Constructor
public function Frame():void {
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
// ☼ --- Init
public function init():void {
fnt = new Fonts(); // init the Fonts class
//fnt.data.avant97 = footFont; // trying to get avant97
Fonts.data.avant97 = footFont; // Changed per 1st answer
trace("footFont = "+footFont); // Fail
footer.graphics.beginFill(0x000);
footer.graphics.drawRect(0,0,800,56);
footer.graphics.endFill();
footer.y = stage.stageHeight - footer.height;
var footText:TextField = new TextField();
footText.defaultTextFormat = footFont; // Fail x 2!
footText.antiAliasType = flash.text.AntiAliasType.NORMAL;
footText.selectable = false;
footText.mouseEnabled = false;
footText.wordWrap = true;
footText.width = 800;
footText.height = 30;
footText.text = footCopy;
// ☼ --- Place Footer & Copy
footer.addChild(footText);
addChild(footer);
trace("Frame added --- √"+"\r");
this.removeEventListener(Event.ADDED_TO_STAGE, init);
}
}
}
基本上我得到的[从这里的静态无功数据对象想法] [2] 但也许他的例子只适用于实际的数据?不TextFormats?
这是我再次遇到了错误: footFont =空 类型错误:错误#2007:参数格式必须为非空
。解决方案
如果你只是想保持你的文本格式与其他代码中分离出来,你应该考虑你的字体类作为“静态”的(不能被实例化)。字体类的所有方法和属性想Fonts.method引用()Fonts.property。
private footFont : TextFormat = Fonts.avant97;
应该注意的是,你不需要,除非你要编辑其属性以创建avant97的副本。你可以简单地代替footFont的使用Fonts.avant97。
其他提示
更改fnt.data到Fonts.data因为它是一个静态变量
不隶属于 StackOverflow