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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:02:43+00:00 2026-05-25T18:02:43+00:00

I am playing with html5 canvas to create bouncing balls. I have this all

  • 0

I am playing with html5 canvas to create bouncing balls. I have this all working but I have to call an initialise function to set certain properties. How can I do this automatically in the constructor without having the initializer firing when the properties are accessed?

var test1 = new Ball(20);
test1.setAngleAndVelocity(); //I dont want to have to call this.

function Ball(speed){
    this.position = new Vector2(canvas.width / 2, canvas.height / 2);
    this.velocity;
    this.speed = speed;
    this.angle;
    this.setAngleAndVelocity = function(){
        this.angle = Math.floor(Math.random() * 360) * 0.0174532925;
        this.velocity = new Vector2(this.speed/10 * Math.cos(this.angle), this.speed/10 * Math.sin(this.angle));
    }
}
  • 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-25T18:02:43+00:00Added an answer on May 25, 2026 at 6:02 pm

    Since setAngleAndVelocity() is a static method, I’d recommend putting it in the prototype of your Ball class:

    function Ball(speed){
        this.position = new Vector2(canvas.width / 2, canvas.height / 2);
        this.speed = speed;
        this.setAngleAndVelocity(); //Sets the additional values
    }
    Ball.prototype.setAngleAndVelocity = function(speed){
        speed = typeof speed != "undefined" ? speed : this.speed;
        this.angle = Math.floor(Math.random() * 360) * 0.0174532925;
        this.velocity = new Vector2(speed/10 * Math.cos(this.angle), speed/10 * Math.sin(this.angle));
    }
    

    this.velocity; and this.angle; aren’t necessary: They’re not defining anything, and the only use they serve is to show the developer what properties may be defined.

    After these modifications, your script has become more efficient, and can be used in this way:

    var test1 = new Ball(20); //Inititalized
    test1.setAngleAndVelocity(22); //Optional, a method to adjust the speed value after the init of the class.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to html5 and have been playing around with the canvas. I'm wondering
I am a graphics novice, but am playing with HTML5 Canvas, javascript and some
I'm playing with the canvas element in HTML5 and I have noticed a peculiar
I have been playing around with the canvas element in HTML5 and I am
How would I write a function that stops ALL instances of playing html5 audio
I'm just started on playing around with the canvas HTML5-object. For the sake of
i'm playing around with html5 video cause it's nice to have fallback for iphone/ipad.
I'm trying to create an offline HTML5 test application, and am playing with the
I've been playing around with the html5 canvas and don't understand how the units
I am developing an HTML5 player on Windows, but I have some real difficulties

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.