AS3动态文本

时间:2014-09-21 10:33:17

标签: actionscript-3 flash actionscript

我是ActionScript 3的新手,我正在尝试在启动计时器时耗尽数字

到目前为止我已经得到它所以你点击按钮'btngo'并且在球对象移动时开始计时器。

我还想要一个动态文本,从100开始,在点击这个按钮的情况下在10秒内耗尽,但不幸的是我不知道怎么做

非常感谢任何帮助,谢谢:)

import flash.utils.Timer;
import flash.events.TimerEvent;

stop();


var timer1:Timer = new Timer (10,10000);
var timer2:Timer = new Timer (10,10000);
timer1.addEventListener(TimerEvent.TIMER, forward);
timer2.addEventListener(TimerEvent.TIMER, back);
btngo.addEventListener(MouseEvent.CLICK, green);
btnstop.addEventListener(MouseEvent.CLICK, red);

function green(e:MouseEvent):void {
    timer2.stop();
    timer1.start();
    trace("timer started");
}

function red(e:MouseEvent):void {
    timer1.stop();
    timer2.start();
    trace("timer started");
}

function forward(e:TimerEvent):void {
    ball.x += 2;
}

function back(e:TimerEvent):void {
    ball.x -= 2;
}

1 个答案:

答案 0 :(得分:0)

嗯,这里有两个功能可以满足您的需求。第一个是创建文本:

import flash.text.TextField;
import flash.text.TextFieldType;

var dynamic_text:TextField;
function CreateText()
{
    // You can also replace this part with code that creates a textfield that you have in your library,
    // in that case just make sure the textfield is set to dynamic in the textfield properties

    // Create a new textfield.
    dynamic_text = new TextField();

    // Make sure its type is set to Dynamic.
    dynamic_text.type = TextFieldType.DYNAMIC;

    // Position it somewhere on the screen.
    dynamic_text.x = 10;
    dynamic_text.y = 10;

    // Set a default text for now.
    dynamic_text.text = "Time: " + time_left;

    // Make sure the players cannot select the text.
    dynamic_text.selectable = false;

    // Add it to your stage (or other parent, take your pick)
    stage.addChild(dynamic_text);
}

每个TimerEvent调用第二个:

function OnTimerChange()
{
    // Update the text based on the new time.
    dynamic_text.text = "Time: " + time_left;
}

您必须在自己的"转发"内部调用此OnTimerChange函数。和"落后"函数,在这两个函数中,你必须计算已经过了多少时间。

有关如何跟踪自按下按钮后经过的时间的示例: actionscript 3 how to keep track of time elapsed?

希望这会有所帮助。