2017-04-10 4 views
0

オーディオがcoffeescriptで終わるときに関数を呼び出そうとしていますが、JavaScriptでコードをテキスト化していますが、完全には動作しますが、coffeescriptでは機能しません。coffeescriptでオーディオonendイベントを作成する方法

ここにcoffeescriptのコード全体があります。

play:() -> 
    if (@getConfig "musicPath") != "../audioclips/backgroundmusics/" 
     @pathtoMusic = @getConfig "musicPath" 
    else 
     @pathtoMusic = path.join(__dirname, @getConfig "musicPath") 

    @musicFiles = fs.readdirSync(@pathtoMusic.toString()) 
    @music = new Audio(@pathtoMusic + @musicFiles[0]) 
    @music.volume = @getConfig "musicVolume" 

    @isPlaying = false if (@music.paused) 
    return null if @isPlaying 

    @isPlaying = true 
    @music.play() 
    @music.onended = -> 
     @music.play() //-----Here doesn't work------// 

私は原子テキストエディタを使用してパッケージ内のcoffeescriptコードをラムし、クロムはjavascriptコードをラムに付けます。

具体的には、原子上でonendイベントを使用するパッケージを見ましたが、ここにコードがあります。

コード・チャンピオン

'use babel'; 

epicVictory() { 
     if (this.isPlaying) return 
     console.log('Epic Victory!'); 
     let maxIndex = this.winFiles.length - 1 
     let minIndex = 0 
     let randomIndex = Math.floor(Math.random() * (maxIndex - minIndex + 1) + minIndex) 
     this.audio = new Audio(this.winpath + this.winFiles[randomIndex]) 

     this.audio.onended =() => { 
      this.audio.play() //-------here work-----// 
      //this.isPlaying = false 
     } 

    this.isPlaying = true 

    this.audio.volume = this.volume 
    this.audio.play() 
    return true 
}, 

私はオンラインコンバータを使用してJavaScriptにバベルコードを変換して、私はこれを得ました。 JavaScriptの

//imput 
this.music.onended =() => { 
    this.audio.play() 
} 

//ouput 
this.music.onended = function() { 
    this.music.play(); 
}; 

バベルそれから私は、コンパイラをCoffeeScriptのためにJavaScriptコードを変換して、私はこれを得ました。

Javascriptを

//imput 
this.music.onended = function() { 
    this.autoPlay(); 
} 

//ouput 
@music.onended = -> 
    @music.play() 

をCoffeeScriptのすることではなく、コードはバベルに取り組んでいます。私は何が間違っているのか分からない。

+0

音声は正常に再生されますが、音声は終了しても何もしません。 –

答えて

1
あなたは「この」バインドするためのCoffeeScriptで脂肪の矢印機能を使用する場合があり

:あなたはコンテキストのもう少しを投稿することができた場合は

@music.onended = => 
@autoPlay() 

、それが役立つかもしれません。

+0

ありがとうございます。 :D –

+0

問題ありません!あなたはそれをソリューションとしてマークしてもらえますか? –

関連する問題