将TextInput对象添加到MovieClip

时间:2013-06-17 19:01:26

标签: actionscript-3 flash-cs5

我有一个屏幕,我想在其中添加一个TextInput对象。这样玩家就可以输入他或她的名字来获得高分。

我该怎么做?我已经阅读过使用T工具添加TextInput并将其拖入。但是如何在Enter-key上将它的值或事件访问到我的HighscoreScreen.as代码中?

我不能以编程方式添加TextInput吗? Flash中不知道TextInput,或者我遗失了什么?

1 个答案:

答案 0 :(得分:1)

您确实可以使用T工具绘制输入文本字段。确保将字段类型设置为Input Text并为其指定实例名称(例如txtName)。然后在您的代码中,您可以像这样访问该字段:

import flash.text.*;
import flash.display.*;
...

// Code class for the score screen.
public class ScoreScreen
{
    private var m_mcScreen:MovieClip = null;
    private var m_txtName:TextField = null;
    ...

    public function ScoreScreen ( mcScreen:MovieClip )
    {
        m_mcScreen = mcScreen;

        InitName ();
    }

    public function InitName ():void
    {
        // This assumes that m_mcScreen has a direct child named 'txtName'
        // If the text field is more deeply nested, you need to navigate
        // through the entire hierarchy of display objects until you
        // reach the text field.
        m_txtName = mcScreen.getChildByName ( "txtName" ) as TextField;
        m_txtName.text = "Default text";
    }
}

我在我的Flash游戏中使用了这个代码 - 我的设置是这样的,我为游戏的每个主要屏幕都有一个专用的影片剪辑,每个都有一个实现整个屏幕逻辑的类。 (我在.fla文件中根本没有代码 - 这对团队工作有很大帮助。)

请注意,我实际上并没有编译这段代码。可能缺少进口或小错误。