嘿伙计们,我需要在我正在处理的flash项目中显示一条消息。消息需要以屏幕为中心,并且只能使用代码创建。
通常我可以放一个动态文本框并隐藏它,但这次我不能。
谢谢, 最大
答案 0 :(得分:3)
我会创建一个以文本字段为中心的类。然后在需要显示消息并将其添加到舞台时创建新实例。这是一个非常简单的例子:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class Message extends Sprite {
private var tf:TextField;
private var s:Sprite;
public function Message(msg:String) {
s = new Sprite();
s.graphics.beginFill(0,0);
s.graphics.drawRect(0,0,800,600);
s.graphics.endFill();
addChild(s);
tf = new TextField();
tf.autoSize = TextFieldAutoSize.CENTER;
tf.width = 400;
tf.text = msg;
tf.x = (s.width-tf.textWidth)*0.5;
tf.y = (s.height-tf.textHeight)*0.5;
addChild(tf);
}
}
}
我会为此添加其他功能,甚至可能添加一个调整大小的侦听器以使其保持居中。您还可以添加几个按钮来关闭消息。添加精灵只是为了禁用邮件下方的任何鼠标点击。这有帮助吗?
答案 1 :(得分:1)
动态地将内容集中在Flash中非常简单;属性stage.stageWidth和stage.stageHeight可用于确定Flash Player画布的大小。附加到displayList的所有DisplayObject都可以访问stage属性;唯一的问题是这个属性在构造函数中将为null,因为它还没有被添加到displayList中;但是,我们可以使用事件监听器轻松解决这个问题:
public class AutoStageCenterSprite extends Sprite
{
public function AutoStageCenterSprite()
{
if (stage == null) {
// Wait to be added to Stage before we activate the Resize Listener.
addEventListener(Event.ADDED_TO_STAGE, onAddedToStageEvent);
}
else {
centerOnStage();
}
}
private function onAddedToStageEvent(event : Event) : void
{
// Now we've been added we can center ourselves...
centerOnStage();
// We will also register for Resize Events so we can update our position
// is the Stage dimensions change.
stage.addEventListener(Event.RESIZE, onResizeEvent);
}
private function centerOnStage() : void
{
x = (stage.stageWidth / 2) - (width / 2);
y = (stage.stageHeight / 2) - (height / 2);
}
private function onResizeEvent(event : Event) : void
{
// This event listener will be tripped each the Stage is resized.
if (stage != null) {
centerOnStage();
}
}
}
问题的下一部分涉及如何将TextField添加到等式中。 The Flash TextField API。您可以选择扩展TextField类并添加居中代码;或者使用组合,并在AutoStageCenterSprite中创建一个新的TextField实例。