0

I'm trying to make my enemy fire every second. Right now - it's every frame.

Within my enemy object, I have a piece of code that is initiated when the player is within range:

this.shotBullet = false;
var object = this;
object.Fire();

This is the enemy fire function:

this.Fire = function(){

    console.debug("Firing | Shot: " + this.shotBullet);

    if(!this.shotBullet){   

        if(this.weapon == "pistol")
            PistolEnemy(this);  

        this.shotBullet = true;

    }

};

And my PistolEnemy function:

PistolEnemy = function(operator){

    var user = operator;

    console.debug("user:" + user.tag);

    var bulletDamage = 1;
    var bulletSpeed = 20;

    var b = new Rectangle( user.x + (user.width / 2) - 4, user.y + (user.height / 2) - 4, 8, 8);
    var velocityInstance = new Vector2(0, 0);

    velocityInstance.x = Math.cos(user.rotation) * bulletSpeed;
    velocityInstance.y = Math.sin(user.rotation) * bulletSpeed;


    var bulletInstance = new Bullet(velocityInstance, b, "Enemy", bulletDamage, "blue");

    /*audioPistol.volume = 0.5;
    audioPistol.currentTime = 0;
    audioPistol.play();*/

    user.bullets.push(bulletInstance);
    user.shotBullet = true;

};

I've tried playing around with the 'setInterval', but it doesn't work well. Most of the times, it waits for a second, then sprays a load of bullets.

All I want it for a enemy bullet to initiate every second.

Thanks

1 Answer 1

1
var triggerID = window.setInterval(function(){firing_clock()},1000);

function firing_clock()
{
// this will execute once per second....sort of
}

Mozilla window.setInteral doc

So, one thing you should known is if your browser gets busy it will get 'late'. Mozilla used to have an extra non-standard parameter detailing "actual lateness", but it no longer does - but the point was that you are asking the browser to try to do something once per second, but if it gets busy it will get behind or skip a few rounds (how the browser handles it differs by browser).

Ideally what you would do here is register your enemy object with a list that firing_clock() would work through to dispatch firing commands to all live enemies. This cuts overhead by only using one global timer, rather than one timer per object on screen.

Try this with just one hard-coded enemy and see how it works. If it still doesn't work, then it's a bigger problem as there is no javascript "guaranteed accurate timer" that I'm aware of.

But it should work, so long as things don't get too intense on the client's CPU, and having one global timer for ship firing should allow you to have a good number of ships firing away without too much ill effect.

Sign up to request clarification or add additional context in comments.

2 Comments

One thing I forget to mention, is that the 'object.Fire();' is in my draw function - so it's called every frame. This, isn't working either, it waits for 1 second, then calls the function over and over again.
Only do something every frame is strictly necessary, partly because as your game grows performance will be an issue, and partly because browsers don't Really run all of certain parts of javascript every frame. Sometimes they act like they do, but to ensure usability they take a fast and loose approach with scripting-based promises and object messaging/execution. In js your control of this is limited, so it's best to try not to ask for something 30+/sec if you only need it 1/sec. When you get in range fire manually, then register with a delay for when you might next want to fire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.