diff --git a/examples/flappy-bird/application.rb b/examples/flappy-bird/application.rb index 4b00d4d..62a7212 100755 --- a/examples/flappy-bird/application.rb +++ b/examples/flappy-bird/application.rb @@ -194,12 +194,22 @@ def reset! Pipe.new(WIDTH * 1/2, HEIGHT/2), Pipe.new(WIDTH * 2/2, HEIGHT/2) ] + @bonus = nil @score = 0 end def play_sound(name) - path = "/_static/#{name}.mp3" - self.script("new Audio(#{JSON.dump(path)}).play()") + self.script(<<~JAVASCRIPT) + if (!this.sounds) { + this.sounds = {}; + } + + if (!this.sounds[#{JSON.dump(name)}]) { + this.sounds[#{JSON.dump(name)}] = new Audio('/_static/#{name}.mp3'); + } + + this.sounds[#{JSON.dump(name)}].play(); + JAVASCRIPT end def play_music