有人可以帮助我定时器和声音事件



所以我试图创建一个标题屏幕,显示和图像并在5秒后消失,然后一旦程序开始播放一首歌。这首歌将贯穿整个游戏。

    public function TitleScreen(){//adds a Title Screen
    var tsBackground:tsBack= new tsBack();
    tsBackground.x= -22
    tsBackground.width=650
    tsBackground.height=450
    addChild(tsBackground);
    var mainTheme:tsTheme = new tsTheme(); 
    mainTheme.addEventListener(Event.COMPLETE, completeHandler); 
    function completeHandler(event:TimerEvent){
    mainTheme.play();
    }
    var counter = 0;
    var myTimer:Timer = new Timer(5000);
    myTimer.addEventListener(TimerEvent.TIMER, TimerFunction)
    function TimerFunction(event:TimerEvent){
        counter++
        removeChild(tsBackground);
        AddStuff();
    }
    myTimer.start();
    /*if (myTimer >= 5000) {
        myTimer.stop();
     }*/
}//end of TitleScreen

我注释掉了if语句,它决定是否停止,因为我得到了这个错误:

1176: Comparison between a value with static type flash.utils:Timer and a possibly unrelated type int.

我遇到的第二个问题是,当mainTheme.play();,我知道我正确地做了链接。

有人能帮忙吗?

错误告诉您正在尝试将myTimer对象(类型为Timer)与整数(5000)进行比较。这就像在问"数字3和这盏灯哪个更大?"我想你的意思是比较你的counter变量,但即使这样也不会像你想要的那样工作。

你的TimerFunction是当你的定时器达到零时运行的函数。所以没有必要做比较。你已经知道5秒什么时候结束了,因为那个函数会运行。你可以在这里停止计时器。您可能还想删除那里的事件侦听器:

function TimerFunction(event:TimerEvent){
    removeChild(tsBackground);
    AddStuff();
    myTimer.stop();
    myTimer.removeEventListener(TimerEvent.TIMER, TimerFunction);
    mainTheme.play();
} 

最新更新