Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:29:20+00:00 2026-06-11T08:29:20+00:00

player.fire is a boolean value that turns true if the spacebar is in keydown

  • 0

player.fire is a boolean value that turns true if the spacebar is in keydown. Everything works as it should.

The problem I have is controlling the array asteroids.firing.push interval. Right now, it adds a few instances, even when I tap lightly on the spacebar. I am using requestAnimationFrame.

How do I control the push interval?

    if(player.fire){
        var angle = thisShip.rot, 
        hyp = 10; //speed

        var vX = Math.cos(angle) * hyp, 
        vY = Math.sin(angle) * hyp;

        asteroids.firing.push(new asteroids.model.fire(thisShip.x, thisShip.y, vX, vY));
    }

I have tried something like this, but it doesn’t slow down the push interval, rather it just creates a pulsing effect, regenerating the firing sequence again and again.

    ...
    if(player.fire){
            fireInterval(thisShip);
    }

    function fireInterval(thisShip){
        var angle = thisShip.rot, 
            hyp = 10; //speed

        var vX = Math.cos(angle) * hyp, 
        vY = Math.sin(angle) * hyp;

        asteroids.firing.push(new asteroids.model.fire(thisShip.x, thisShip.y, vX, vY));

        setTimeout(function(){
            fireInterval(thisShip);
        }, 500);
    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:29:22+00:00Added an answer on June 11, 2026 at 8:29 am

    What’s happening is you’re calling this function every update.
    I don’t know what your engine is like, but that might be up to 60fps if you’re using requestAnimationFrame, or more if you’re just looping as quickly as possible, and updates aren’t tied to drawing.

    What you need to do is put some sort of state on the ship’s gun.
    It doesn’t matter what it is…

    thisShip.cooldownTime = 250; //ms
    thisShip.lastFired =    oldTimestamp;
    
    if (currentTime - thisShip.lastFired >= thisShip.cooldownTime) {
        thisShip.fire(); // push the model, set lastFired to current time, etc...
    }
    

    Also, in terms of what the other people are suggesting:

    I’m not sure how you’ve implemented listening to keyboard-events…
    But handling keys in JS, what I’d be most-inclined to do is let the keyboard fire as many keydown and keyup events as it wants.
    Instead of buffering that, have the only thing the key-event function does is tell a Keyboard object whether a key is down, or whether it is up.

    Poll that Keyboard object during your update.

    document.addEventListener("keydown", function (e) {
        var key = e.keyCode;
        if (!!Keyboard[key]) { return; } 
        Keyboard[key] = e.timeStamp;
    });
    
    
    document.addEventListener("keyup", function (e) {
        var key = e.keyCode;
        delete Keyboard[key];
    });
    

    Poll to see if the key is down.
    By setting the value of the key to e.timeStamp, you now know when they started holding it down, so you can gate how often your ship fires.

    Better, you can set up a few abstracted systems, like:

    var Keymap = { SPACE : 32, A : 65, ... };
    Player1.controls = { FIRE : "SPACE" };
    
    var code = Keymap[Player1.controls.FIRE],
        pressedSince = Keyboard[code]; // timestamp || undefined
    

    Now you have something you can compare to your cooldown period and gates.
    It won’t matter how fast they tap, or if keyboards work via turbo-fire by default, you should have all of the data-points required to prevent turbo-fire, to allow held-down auto-fire (gated at 250ms, or gated to having only 3 bullets onscreen, or whatever), and gated to a smaller value if you detect that the key keeps being tapped, to let button-mashers have a slight edge over button-holders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Objective-C I have a timer fire every 0.1 seconds and increment a double value
I have a very simple problem. I have a player on my website which
I've got an HTML5 audio player that works on Safari on the PC, but
I have a while loop that loops until bool done = true; In the
I have a problem with three clases. The the first class is called Player
A player: http://www.yvoschaap.com/videowall/ How can you customise the above Chromeless Youtube to have Play/Stop/Pause
Flow player claims that it supports all file formats. Do the video files need
I have a media player I've created in C, and its running along side
The video player actually loads fine. My actual problem is when I refresh some
I have a baseball player super class I have a left field, center field,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.