【问题标题】:How to create a button that makes a new text field appear on screen?如何创建一个使新文本字段出现在屏幕上的按钮?
【发布时间】:2016-01-10 03:10:06
【问题描述】:

我一直无法让这个按钮工作。我想要它,以便每当您单击它时,都会弹出一个新的文本字段,您可以在其中输入内容。这应该是用于类似琐事的游戏,用户可以在其中输入自己的问题和答案。这是到目前为止的代码...

这是文本字段的属性 -

    QuestionTextField = new TextField();
            QuestionTextField.defaultTextFormat = SmallTextFormat;
            QuestionTextField.textColor = 0x660000;
            QuestionTextField.x = stage.stageWidth * 0.1;
            QuestionTextField.y = 200;
            QuestionTextField.width = stage.stageWidth * 0.8;
            QuestionTextField.height = 20;
            QuestionTextField.selectable = false;
            QuestionTextField.type = TextFieldType.INPUT;
            QuestionTextField.border = true;
            QuestionTextField.multiline = true;
            QuestionTextField.wordWrap = true;

这是每次单击按钮时都应该创建一个新文本字段的函数-

     private function AddQuestionButtonClicked(m:MouseEvent){
        for(var i = Questions.length; i>=1; i--){
            container_buttonsMC.addChild(QuestionTextField);
            Questions.push(QuestionTextField);
            QuestionTextField.y += 20
        }
     }

注意:“问题”是一个包含文本字段数量的数组,container_buttonsMC 包含游戏主菜单上的所有按钮。

问题是,每当我点击它时,它 1. 实际上并没有创建一个新的文本字段,而是将它向下移动了一定数量的像素,以及 2. 它有点复制了文本字段的位置...如果这有任何意义的话。如果我单击一次按钮,它会将文本字段向下移动 20 像素。如果我再次单击它,它将向下移动 40 像素,然后向下移动 80 像素,依此类推...

我还跟踪了数组的长度,令人惊讶的是,每次单击它,出现的数字都会翻倍。但是,只有一个文本字段可见(显然)。它可能与 for 循环的操作有关,但我不确定。有谁知道问题是什么? (提前感谢,我对 AS3 和一般编程还是新手)

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    首先,每次单击按钮时都需要创建一个新的文本字段,而不是将其添加到舞台上。就目前的情况而言,您只创建了一个文本字段。

    其次,您只想再添加一个文本字段,对吗?因此,不要使用会创建多个新文本字段的循环,只需创建一个额外的文本字段并将其添加到列表中即可。

    代码看起来像

    private function AddQuestionButtonClicked(m:MouseEvent){
        var QuestionTextField:TextField = new TextField()
        QuestionTextField.defaultTextFormat = SmallTextFormat;
        QuestionTextField.textColor = 0x660000;
        QuestionTextField.x = stage.stageWidth * 0.1;
        QuestionTextField.y = 200;
        QuestionTextField.width = stage.stageWidth * 0.8;
        QuestionTextField.height = 20;
        QuestionTextField.selectable = false;
        QuestionTextField.type = TextFieldType.INPUT;
        QuestionTextField.border = true;
        QuestionTextField.multiline = true;
        QuestionTextField.wordWrap = true;
    
        // You can adjust the 50 here to control the spacing between text fields
        QuestionTextField.y += 50 * Questions.length
        container_buttonsMC.addChild(QuestionTextField);
        Questions.push(QuestionTextField);
    }
    

    【讨论】:

    • 这解决了数组问题,但是每当我单击按钮时,预先存在的文本字段就会消失。它删除文本字段,更改其 y 值,然后再次添加。
    • 嗯。你确定new TextField() 的那一行在里面吗?有什么可以删除文本字段的吗?
    • 你能把这个加到最后并告诉我会发生什么吗? if (Questions.length > 1) { trace(Questions[0] === Questions[1]); }
    • 我想我发现了一些东西...文本字段没有被删除,只是看起来有一个,因为它们堆叠在一起...
    • 另外,你让我添加的那个东西给了我 1009 错误。
    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2020-02-21
    • 2016-03-04
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多