尝试将 TextFormat 传递到其他类中,出现错误
大家星期二快乐 :)
我注意到我在几个子类中再次使用相同的字体,所以我想我只需创建一个字体类来处理所有这些。
无论如何,我正在绞尽脑汁地思考如何将我的 Fonts 类中创建的 TextFormats 干净地放入我的其他类中。我不相信我以正确的方式执行此操作,但目前我收到此错误消息:
footFont = null TypeError: 错误 #2007: 参数格式必须为非空。
我想将 avant97 TextFormat 传递到我的 Frame 类中以设置页脚文本的样式。
下面是我的 Fonts 类。
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;
}
}
这是我的 Frame 类,我试图将 avant97 附加到TextField:
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);
}
}
}
基本上我从这里得到了[静态 var 数据对象的想法][2] 但也许他的例子只适用于实际数据?不是文本格式?
这是我再次遇到的错误: footFont = null TypeError:错误#2007:参数格式必须为非空。
Happy Tuesday everyone :)
I noticed I was using the same fonts over again in several of my Sub Classes, so I figured I'd just make a Fonts Class to handle all those.
Anyways I'm scratching my head around how to get those TextFormats created in my Fonts class into my other Classes cleanly. I don't believe I'm doing this the correct way, but currently I'm getting this error msg:
footFont = null
TypeError: Error #2007: Parameter format must be non-null.
I want to pass the avant97 TextFormat into my Frame Class to style the Footer text
Below is my Fonts Class.
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;
}
}
Here is my Frame class where I'm trying to attach avant97 to a TextField:
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);
}
}
}
Basically I got the [idea for the static var data object from here][2]
But maybe his example only works for actual data? Not TextFormats?
This is the Error I'm getting once again:
footFont = null
TypeError: Error #2007: Parameter format must be non-null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只是想将文本格式与其他代码分开,则应该将 Fonts 类视为“静态”(不应实例化)。 Fonts 类的所有方法和属性都将像 Fonts.method() Fonts.property 一样被引用。
应该注意的是,您不需要创建 avant97 的副本,除非您要编辑其属性。您可以简单地使用 Fonts.avant97 代替 footFont。
If you're just trying to keep you text formats separate from other code, you should think of your Fonts class as "static" (should not be instantiated). All methods and properties of the Fonts class would be referenced like Fonts.method() Fonts.property.
It should be noted that you don't NEED to create a copy of avant97 unless you're going to edit its properties. You could simply use Fonts.avant97 in lieu of footFont.
将 fnt.data 更改为 Fonts.data,因为它是静态变量
Change fnt.data to Fonts.data because it's a static variable