有人可以用计时器和声音事件来帮助我吗?

时间:2015-10-21 15:52:58

标签: actionscript-3 actionscript

所以我试图创建一个标题屏幕,在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.

我遇到的第二个问题是当主题Theme.play()时歌曲没有播放;被调用,我知道我正确地进行了联系。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

您的TimerFunction是您的计时器达到零时运行的功能。所以没有必要进行那种比较。您已经知道5秒钟何时启动,因为该功能将运行。所以你可以在那里停止计时器。您可能还想删除那里的事件监听器:

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