Jump to content

Counting Beats


ThatGirlDee
 Share

Recommended Posts

Hi I'm new to Javascript and trying to learn Phaser by building a simple rhythm game. in short you tap along to the beat of a song. You know when to tap by a horn sound. I have created a function that calculates when the sound should play in the given time in minutes and stores it to an array like this:

function songManager(evt) {       this.notes = [];     storage['currentBpm'] = evt.bpm    storage['eighthNoteDuration'] =  1000 * 0.5 * 60 /evt.bpm ;     this.totalBars = (storage['eighthNoteDuration'] * 32)    / storage['totalTime'] ;                // Set global notes array    for (var bar =0; bar < this.totalBars; bar ++){            //console.log(bar);        var noteTime = bar * 8 * storage['eighthNoteDuration'];        this.notes.push(noteTime + 2 *storage['eighthNoteDuration'], noteTime + 8 * storage['eighthNoteDuration'] );            }                         storage['notes'] = this.notes;        function calculateBar(evt) {            this.notes.push(evt );    }

 I'm having trouble keeping track of time in my game.js and comparing it to the time in my array. I start the song and the timer:

  songStart: function() {                this.previousTime = this.song.currentTime;        this.song.play();        this.lastReportedPlayheadPosition = 0;        this.songPlaying = true;            },

and then update the time in the update function: 

       this.songTime =  this.song.currentTime - this.previousTime;        this.previousTime = this.song.currentTime;

then I check to play the horn in a function called every update also :

 tapActive: function() {            if(this.songPlaying == true){            for ( var i =0; i <  storage['notes'].length; i++){                if (this.songTime == storage['notes'][i] ) {                                        this.horn.play();                    this.input.onDown.addOnce(this.isDown,this);                }             else {                    this.input.destroy();                }            }        }    },

At the moment my horn does not play at all and the input never turns active. I know I might be asking a lot but any suggestions or a better idea of how to do this would be much appreciated.  Thank You! 

Link to comment
Share on other sites

How are you calculating the current time? Is the problem that the condition 'if (this.songTime == storage['notes'][i] )' is never met? In which case I'd log out the songTime value and see what sort of values you're getting, do they ever equal the storage times? Maybe if you're using real time values to calculate this then you need a range comparison, not a strict one.

Link to comment
Share on other sites

I'm counting the current time by using Phaser.sound.currentTime to store the time the song starts and then every update setting songTime like this:  

this.songTime = song.currentTime - this.previousTime;this.previousTime = this.song.currentTime;

when I tried to log out the songTime value every update it shows a weird loop where every other update it sets to 0 

CURRENT Time: 34CURRENT Time: 0CURRENT Time: 94CURRENT Time: 0CURRENT Time: 73CURRENT Time: 0CURRENT Time: 69CURRENT Time: 0

what is the difference between sound.currentTime and sound.position? Is there a more accurate way of storing the milliseconds the song is currently at or should I be using fps instead of milliseconds ? Thank you for your help! 

 

Edit: So I solved the sound not playing by setting my songTime var to only update if the song was playing and also updating the var with += rather than equals which was just resetting it to 1 every time. Rich was also right and a range comparison worked I used: 

if (this.songTime % storage['eighthNoteDuration'] == 0 && this.songTime == storage['notes'][i] ) 
Edited by ThatGirlDee
Link to comment
Share on other sites

Sound.currentTime is a ms value based on the current system clock time minus the time the sound started playing. If the sound loops the currentTime is reset to zero again.

 

I don't know how long your sounds are, so it's hard to tell if the above output is correct or not.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...