nightwalker

Trouble getting started with rhythm game

Recommended Posts

Hey everyone. I'm fairly new to Phaser. I'm making a rhythm game (similar to Osu!), but I'm having a problem getting my sprites to spawn to the beat of the song and am not sure what I'm doing wrong.

Here is what I've attempted:

var renegade =
{
    //variables
    //bpm = beats per minute, bps = beats per second
    //spb = seconds per beat
    
    bpm : null,
    bps : null,
    startTime : null,
    songLength : null,
    currentTime : null,
    spb : null,
    lastBeatTime : null,
    blackHitElement : null,
    music : null,
    
    preload : function()
    {
        game.load.image("gangsta","assets/gangsta.png");
        game.load.image("black","assets/hit-element-black.png");
        
        game.load.audio("music", "assets/Renegade.mp3");
    },
    
    create : function()
    {
        bpm = 135;
        bps = 2.25;
        songLength = 243;
        currentTime = songLength;
        spb = 0.4;
        lastBeatTime = 0;
        
        game.add.sprite(0,0,"gangsta");
        //blackHitElement = game.add.sprite(400, 500, "black");
        
        //currentTime += dt;
        
        /**if(currentTime - lastBeatTime < spb)
      {
        if( 0.8 * spb < curentTime / bps < 1.2 * spb)
      {
          lastBeatTime = currentTime;
          blackHitElement = game.add.sprite(400, 500, "black");
      }
      }**/

        music = game.add.audio("music");
        music.play();
        
    },
    
    update : function()
    {
        currentTime += dt;
        
        if(currentTime - lastBeatTime < spb)
      {
        if( 0.8 * spb < curentTime / bps < 1.2 * spb)
      {
          lastBeatTime = currentTime;
          blackHitElement = game.add.sprite(400, 500, "black");
      }
      }
    },
};

var game = new Phaser.Game(850,850, Phaser.AUTO, "gameArea", renegade);

 

If anyone can point me in the right direction, I would greatly appreciate it!

Share this post


Link to post
Share on other sites

I agree with @samme

In this example you can see how it works:

https://phaser.io/examples/v2/time/basic-looped-event

On the other hand, if you are going to create many sprites, it may be more efficient to create a group of sprites in the create function and reuse them, like in this example:

https://phaser.io/examples/v2/groups/recycling

Surely it will be a transcription error, but in your code the variable "dt" is not assigned, and the rest of the variables of the create function should have the prefix "this", unless they are global variables. 

Good luck with your project!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.