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 7068637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:20:54+00:00 2026-05-28T05:20:54+00:00

I am developing a pong redux in html5 and javascript. I need help modifying

  • 0

I am developing a pong redux in html5 and javascript. I need help modifying it so that upon holding down D, it will subtract 4 from the ball.speedModifier variable, and then add 4 upon releasing D. The reason I don’t just set the modifier back to 0 when the key is released, is because I want to have multiple things changing the modifier at once that stack on top of one another. With my current system for detecting key presses, the addition and subtraction of the variable would repeat at 60 FPS, while I only want it to occur once until the amount is subtracted again.

Here is a pastie of my code:
HTML: http://pastebin.com/txeNftNT
Javascript: http://pastebin.com/scpBqGqx

  • 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-05-28T05:20:54+00:00Added an answer on May 28, 2026 at 5:20 am

    I think the easiest way to do this would be to track additional properties for each key. Instead of checking just if a key is down or up, you also want to track if they key was down or up on the previous iteration of checkInput(). That way, if the “D” key is down, but it wasn’t down the last iteration, you subtract 4 from ball.speedModifier. Alternatively, if the “D” key is up, but wasn’t up on the last iteration, you then add 4 back to ball.speedModifier.

    The final key to making this work is at the end of every checkInput loop, you save out whether each key was down or not. Then on the next loop of checkInput() you’re ready to do your checks again.

    The following is just pseudo code, but hopefully gives the idea.

    function checkInput(){
    if(d.isDown()){
         if(!d.wasDown()){
              ball.speedModifer -= 4;
         }
    }else{
         if(d.wasDown()){
              ball.speedModifier += 4;
         }
    }
    
    d.wasDown = d.isDown;
    }
    

    Just in case, I also put together a working example. Do note the three changes. Your pressedKeys is no longer an array, but an object of KeyCodes which correspond to the VALUES of your existing KEY object. And then this key’s value is an object itself with two keys of “wasDown” and “isDown”. I then pre-populate this object at the end of init() and then perform your various checks and updates in checkInput using the new object instead of the array.

    http://jsfiddle.net/gUgGf/

    Hopefully that helps!

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

Sidebar

Related Questions

Developing for iPhone, I have a collection of points that I need to make
Developing an application for Android, i want to record data that will be usefull
Developing a Sencha Touch MVC app that pulls data from json store (thats set
Developing a project of mine I realize I have a need for some level
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
I am currently developing a simple Pong game for the iPhone. Currently using CGRectIntersectsRect
i am trying out a pong game to start out developing games on the
Developing rails has become lots of fun over the year that I've done it,
developing an application in Access. The access file will be located on a db
I'm developing an online pong game in which two players could play between them.

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.