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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:44:56+00:00 2026-06-13T23:44:56+00:00

How can I use the code I have now with an object where I

  • 0

How can I use the code I have now with an object where I can store the number of times the ball bounces and the color (when i add random color) and speed. Any pointers or tips would be greatful. I am new to OOP and it can get confusing for me. Thanks in advance

  float x;
  float y;
  float yspeed = 0;
  float xspeed = 0;
  float balldiameter = 10;
  float ballradius = balldiameter/2;

  void setup() {
  size (400,400);
  background (255);
  fill (0);
  ellipseMode(CENTER);
  smooth();
  noStroke();
  x = random(400);
  y = 0;
  }

  void draw() {
  mouseChecks();
  boundaryChecks();
  ballFunctions();
  keyFunctions();
  }

  void mouseChecks() {
    if (mousePressed == true) {
    x = mouseX;
    y = mouseY;
    yspeed = mouseY - pmouseY;
    xspeed = mouseX - pmouseX;
    }
  }

  void boundaryChecks() {
    if (y >= height - ballradius) {
      y = height - ballradius;
      yspeed = -yspeed/1.15;
    }
    if (y <= ballradius) {
      y = ballradius;
      yspeed = -yspeed/1.35;
    }
    if (x >= width -ballradius) {
      x = width -ballradius;
      xspeed = -xspeed/1.10;
    }
    if (x <= ballradius) {
      x = ballradius;
      xspeed = -xspeed/1.10;
     }
   }

   void ballFunctions() {
   if (balldiameter < 2) {
     balldiameter = 2;
     }
   if (balldiameter > 400) {
     balldiameter = 400;
     }
   ballradius = balldiameter/2;
   background(255); //should this be in here?
   ellipse (x,y,balldiameter,balldiameter);
   yspeed = yspeed += 1.63;
    // xspeed = xspeed+=1.63;
   y = y + yspeed;
   x = x + xspeed; 
   }
  void keyFunctions() {
    if (keyPressed) {
      if(keyCode == UP) {
      balldiameter +=1;
    }
    if (keyCode == DOWN) {
      balldiameter -=1;
      }
    }
   }
  • 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-13T23:44:58+00:00Added an answer on June 13, 2026 at 11:44 pm

    you will probably want to do the following:
    create a new file called Ball.pde
    In that file write:

    public class Ball {
        public float x;
        public float y;
        public float yspeed;
        public float xspeed;
        public float diameter;
        public float radius;  
    
        public Ball(float initial_x, float initial_y, float diam) {
            this.x = initial_x;
            this.y = initial_y;
            this.xspeed = 0;
            this.yspeed = 0;
            this.diameter = diam;
            this.radius = diam/2;
        }
    
        public void move() {
           // movement stuff here
        }
    }
    

    This will give you a very basic Ball class. You can now use this class in your main sketch file like so:

    Ball my_ball = new Ball(50, 50, 10);
    

    you can access the balls members using:

    my_ball.xspeed;
    my_ball.yspeed;
    my_ball.anything_you_defined_in_ball;
    

    This will allow you yo store all relevent variables for the ball within its own class. you can even create more than 1.

    Ball my_ball1 = new Ball(50, 50, 10);
    Ball my_ball2 = new Ball(20, 20, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how exactly I can use public methods (non-virtual) with NHibernate? I have this code:
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
Can I have good example or code about how to use Ajax Datepicker in
Can I use the code generated by flex/bison|lex/yacc in a multithreaded environment ? I'm
Can anyone explain why you can’t use inline code blocks within server control declarations
With CLLocationManager I can use the following code to determine if I can access
Is there an ide/site that I can use to analyse code below and get
The Current Setup: So, I can use the following code to save a file
You can use the URL helper in Code Igniter to load CSS and Javascript
I can use the PRINT statement in a stored procedure to debug my code.

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.