无法停止此计时器

时间:2013-02-20 09:21:38

标签: actionscript-3 flash

创建按钮并命名为“b”。 创建动态文本并命名为“tampil”。 运行它...单击b按钮运行计时器,再次单击停止(但错误)。

以下脚本是我修改过的项目的一部分。 我的问题是,如何停止计时器?

import flash.events.MouseEvent;
var xx:Number = 1;
var waktux:Timer;
var i1:Number = 1;
var ab:Number = 1;
var lantaii:String = "lg";
b.addEventListener(MouseEvent.CLICK, stopp);
function stopp(e:MouseEvent)
{
    waktu(0,0,ab);
    if (ab==1)
    {
        ab = 0;
    }
    else
    {
        ab = 1;
    }
}
function waktu(e, d, cx)
{
    var waktux:Timer;
    var waktuy:Timer;
    function sayHello(f:TimerEvent):void
    {
        tampil.text = String(e);
        e = e + 1;
    }
    function sayHellow(e:TimerEvent):void
    {
        tampil.text = String(d);
        d = d + 1;
    }
    function sayHello2(f:TimerEvent):void
    {
        tampil.text = String(e);
        e = e + 1;
    }
    if (cx==1)
    {
        if (lantaii == "lg")
        {
            naek();
        }
        else
        {
            waktuy = new Timer(400,10);
            waktuy.addEventListener(TimerEvent.TIMER, sayHellow);
            waktuy.start();
            waktuy.addEventListener(TimerEvent.TIMER_COMPLETE, naek2);
        }
        function naek()
        {
            waktux = new Timer(400,10);
            waktux.addEventListener(TimerEvent.TIMER, sayHello);
            waktux.start();
        }
        function naek2(s:TimerEvent):void
        {
            waktux = new Timer(400,10);
            waktux.addEventListener(TimerEvent.TIMER, sayHello2);
            waktux.start();
        }
    }
    else
    {
        trace("nih masuk");
        waktux.stop();
        waktux.reset();
        waktuy.reset();
        waktux.removeEventListener(TimerEvent.TIMER, sayHello);
        waktux.removeEventListener(TimerEvent.TIMER, sayHello2);
        waktuy.removeEventListener(TimerEvent.TIMER, sayHellow);
        waktuy.removeEventListener(TimerEvent.TIMER_COMPLETE, naek2);
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你在全球范围内重新定义waktux,在waktu函数中重新定义waktux。 删除其中一行

import flash.events.MouseEvent;
var xx:Number = 1;
var waktux:Timer; //<--------------------------- HERE
var i1:Number = 1;
var ab:Number = 1;
var lantaii:String = "lg";

function waktu(e, d, cx)
{
    var waktux:Timer; //<--------------------------- HERE
    var waktuy:Timer;
    function sayHello(f:TimerEvent):void
    {
        tampil.text = String(e);
        e = e + 1;
    }
    function sayHellow(e:TimerEvent):void
    {
        tampil.text = String(d);
        d = d + 1;
    }
    function sayHello2(f:TimerEvent):void
    {
        tampil.text = String(e);
        e = e + 1;
    }
    if (cx==1)
    {
        if (lantaii == "lg")
        {
            naek();
        }
        else
        {
            waktuy = new Timer(400,10);
            waktuy.addEventListener(TimerEvent.TIMER, sayHellow);
            waktuy.start();
            waktuy.addEventListener(TimerEvent.TIMER_COMPLETE, naek2);
        }
        function naek()
        {
            waktux = new Timer(400,10);
            waktux.addEventListener(TimerEvent.TIMER, sayHello);
            waktux.start();
        }
        function naek2(s:TimerEvent):void
        {
            waktux = new Timer(400,10);
            waktux.addEventListener(TimerEvent.TIMER, sayHello2);
            waktux.start();
        }
    }
    else
    {
        trace("nih masuk");
        waktux.stop();
        waktux.reset();
        waktuy.reset();
        waktux.removeEventListener(TimerEvent.TIMER, sayHello);
        waktux.removeEventListener(TimerEvent.TIMER, sayHello2);
        waktuy.removeEventListener(TimerEvent.TIMER, sayHellow);
        waktuy.removeEventListener(TimerEvent.TIMER_COMPLETE, naek2);
    }
}

只是为了好玩

function stopp(e:MouseEvent)
{
    waktu(0,0,ab);
    ab = !ab;
}