Jump to content

wave movement


mrdotb
 Share

Recommended Posts

Hi everyone,

I want to add a wave movement to my bullet i use this:

if (APP.player.alive === true) {        if (APP.fireButton.isDown)        {            angle = 0;            fireBullet();        }    }function fireBullet() {    //  To avoid them being allowed to fire too fast we set a time limit    if (game.time.now > APP.bulletTime) {        //game.sound.play('fire');        APP.bulletTime = game.time.now + APP.bulletRate;        //  Grab the first bullet we can from the pool        var currentBullet = APP.bullets.getFirstExists(false);        if (currentBullet)        {            currentBullet.revive();            currentBullet.lifespan = 2000; //kill after 2000ms            if (APP.facing === "right") {                //  And fire it                currentBullet.reset(APP.player.x + 15, APP.player.y + 15);                currentBullet.body.velocity.x = APP.bulletvelocity;                game.time.events.repeat(25, 80, function() {                    currentBullet.body.velocity.y = Math.sin(angle) * 20;                    angle += 0.1;                    console.log(angle);                }, this);            }            else if (APP.facing === "left") {                currentBullet.reset(APP.player.x, APP.player.y + 15);                currentBullet.body.velocity.x = -APP.bulletvelocity;                currentBullet.body.velocity.y = APP.bulletY;            }        }    }}

If a fire too quickly they share the same angle.

How to define a different var angle for each bullets.

 

Thank you.

Link to comment
Share on other sites

Instead of using a custom timer and angle var, just try Math.sin(game.time.now) which will give you a steadily increasing number that doesn't change with framerate. You may need to divide the time by some value to reduce the frequency, multiply the resulting sine by another value to give you the amplitude you require, but with a bit of fine tuning you'll end up with a nice framerate independent wave.

 

If on the other hand what you want is for faster firing to have a higher frequency wave, you'll need to include the time since the last shot was fired into the equation somewhere.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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