显示当前和过去的操作投掷文本

时间:2014-07-10 17:43:18

标签: actionscript-3 flash

所以我设置了一组文字来显示你在游戏中完成的最后一个动作。我设法让它添加当前文本下面的文本,但我如何向上移动所有以前的文本,所以每次添加一个新文本,它将把它放在同一个地方,只移动前一个文本,然后删除显示一定数量后的最旧文本

这是游戏 http://www.fastswf.com/F7ei81E

这是我目前使用的代码

    protected function createEventText()
    {
        chatScreen = new Sprite();
        chatScreen.x = 280;
        chatScreen.y = 520;
        addChild(chatScreen);

        addText("The world has spawned!");

    }



    public function addText(t:String)
    {
        eventArray.push(t);
        for (var i:int = eventArray.length - 1; i < eventArray.length; i++)
        {
            et = new EventTextMC();
            et.txt.text = String(eventArray[i]);
            et.y = i * 20;
            chatScreen.addChild(et);
        }

    }

1 个答案:

答案 0 :(得分:1)

我建议您在EventTextMC上保留有限数量的chatScreen个实例。然后,当您添加文本时,您可以将文本从顶部字段“推”到底部字段。

private var _historyLength:int = 5; // number of text fields to generate
private var _textFields:Array = []; // array to keep track of textfields

// create the finite number of fields (based on historyLength)
private function initLog():void 
{
    // adding 5 (historyLength) text fields to the chatScreen
    for(var i:int = 0; i < historyLength;i++)
    {
        var et:EventTextMC = new EventTextMC();
        et.y = i * 20;
        textFields.push(et);
        chatScreen.addChild(et);
    }
}

public function addText(t:String)
{
    eventArray.push(t); // this array isn't actually being used in this example

    // starting with the last text field, copy the previous field's text
    // this will create a "pushing down" effect
    // CLARIFICATION:
    // The text from the second to last field will be placed into the last field.
    // The text from the third to last field will be placed into
    // the second to last field.
    // This will continue until the text from the first field will be placed
    // in the second field, freeing up the first field for the new text.
    for(var i:int = historyLength-1; i > 0; i--)
    {
        textFields[i].txt.text = textFields[i-1].txt.text;
    }
    // add the new text to the first (top-most) textfield
    textFields[0].txt.text = t;
}

虽然我在这个示例中实际上并没有使用eventArray,但您可以在以后轻松地使用它来“滚动”日志。