Getting the balls to constantly move when they are clicked on them

edited April 2014 in Questions about Code
Boolean IsMouseInCircle;
Boolean IsMousePressed;
PVector MyCoor[];
PVector CircleSize;
PVector MousePos;
int CircleNumber;

float diameter[];

float CircleSpeed_Y[];

int counter;  

import ddf.minim.*;
Minim minim;
AudioPlayer Boing;

void setup() {
  size(1024, 700);
  frameRate(60);

   minim = new Minim(this);
   Boing = minim.loadFile("Boing.mp3");

  CircleNumber = 6;

  MyCoor = new PVector[CircleNumber];
  diameter = new float[CircleNumber];
  CircleSpeed_Y = new float[CircleNumber];
  int counter = 0;

  IsMouseInCircle = false;
  IsMousePressed = false;


  while(counter < CircleNumber){

    diameter[counter] = 80;
    float xp = diameter[counter] + counter * 170;
    float yp = diameter[counter] ;

    CircleSpeed_Y[counter] = 20;
     MyCoor[counter] = new PVector(xp,yp);

    counter = counter + 1;

    }



}

void draw(){



  background(255,255,255);
  counter = 0;


  while(counter < CircleNumber){

    if(checkMouseInZone(MyCoor[counter], diameter[counter]/2)){
        if( IsMousePressed){

          MyCoor[counter].y = MyCoor[counter].y + CircleSpeed_Y[counter];
           fill(200,255,0);

             if(MyCoor[counter].y > height) {
                   MyCoor[counter].y = height;
                   CircleSpeed_Y[counter] = - CircleSpeed_Y[counter]  ;
                        println("too far bottom");
                         Boing.pause();
                         Boing.rewind();
                         Boing.play();

                }

             if(MyCoor[counter].y < 0) {
                   MyCoor[counter].y = 50;
                   CircleSpeed_Y[counter] = -CircleSpeed_Y[counter] ;
                        println("too far top");
                         Boing.pause();
                         Boing.rewind();
                         Boing.play();
                     }

                }
        }else{fill(100,100,100);}





   ellipse(MyCoor[counter].x, MyCoor[counter].y, diameter[counter], diameter[counter]);
    fill(0,200,0);


  counter = counter + 1;
  }



}












boolean checkMouseInZone(PVector p, float radius){

                                                                        //this is my function to check if the mouse is in range of the button
  PVector m = new PVector(mouseX, mouseY);  // a temporary PVector to store the current mouse position
  return (m.dist(p) <= radius); // returns true or false

}  


void mousePressed(){


  IsMousePressed = true;
}



void mouseReleased(){

  IsMousePressed = false;
}

Answers

  • Answer ✓

    here....

    Boolean IsMouseInCircle; 
    Boolean IsMousePressed; 
    PVector MyCoor[]; 
    PVector CircleSize; 
    PVector MousePos; 
    int CircleNumber;
    
    float diameter[];
    
    boolean isMoving[];
    
    float CircleSpeed_Y[];
    
    int counter;
    
    import ddf.minim.*; 
    Minim minim; 
    AudioPlayer Boing;
    
    void setup() { 
      size(1024, 700); 
      frameRate(60);
    
      minim = new Minim(this); 
      //  Boing = minim.loadFile("Boing.mp3");
    
      CircleNumber = 6;
    
      MyCoor = new PVector[CircleNumber]; 
      diameter = new float[CircleNumber]; 
      isMoving = new boolean[CircleNumber];
    
      CircleSpeed_Y = new float[CircleNumber]; 
      int counter = 0;
    
      IsMouseInCircle = false; 
      IsMousePressed = false;
    
      while (counter < CircleNumber) {
        diameter[counter] = 80;
        float xp = diameter[counter] + counter * 170;
        float yp = diameter[counter] ;
    
        CircleSpeed_Y[counter] = 20;
        MyCoor[counter] = new PVector(xp, yp);
    
        counter = counter + 1;
      }
    }
    
    void draw() {
    
      background(255, 255, 255); 
      counter = 0;
    
      while (counter < CircleNumber) {
    
        if (checkMouseInZone(MyCoor[counter], diameter[counter]/2)) {
          if ( IsMousePressed ) {
    
            // mark as moving 
            isMoving[counter] = true;
          } // if
        } // if 
        else {
          fill(100, 100, 100);
        } // else 
    
        // -----------------
    
        if (isMoving[counter]) {  
    
          MyCoor[counter].y = MyCoor[counter].y + CircleSpeed_Y[counter];
    
          fill(200, 255, 0);
    
          if (MyCoor[counter].y > height) {
            MyCoor[counter].y = height;
            CircleSpeed_Y[counter] = - CircleSpeed_Y[counter]  ;
            println("too far bottom");
            //          Boing.pause();
            //          Boing.rewind();
            //          Boing.play();
          }
    
          if (MyCoor[counter].y < 0) {
            MyCoor[counter].y = 50;
            CircleSpeed_Y[counter] = -CircleSpeed_Y[counter] ;
            println("too far top");
            //          Boing.pause();
            //          Boing.rewind();
            //          Boing.play();
          }
        } // if
    
        ellipse(MyCoor[counter].x, MyCoor[counter].y, diameter[counter], diameter[counter]); 
        fill(0, 200, 0);
    
        counter = counter + 1;
      } // while
    }
    
    boolean checkMouseInZone(PVector p, float radius) {
    
    
      //this is my function to check if the mouse is in range of the button
    
      PVector m = new PVector(mouseX, mouseY); 
      // a temporary PVector to store the current mouse position 
      return (m.dist(p) <= radius); // returns true or false
    }
    
    void mousePressed() {
    
      IsMousePressed = true;
    }
    
    void mouseReleased() {
    
      IsMousePressed = false;
    }
    
  • edited April 2014

    the idea is to have another array to store if this ball is moving permanently

    it's like a marker for each ball

    remarks

    1.

    instead of while you can also use a for-loop

    2.

    you approach uses several arrays that are parallel: each ball has properties distributed over several arrays (always at the same index) - to overcome this you can turn to OOP / classes

    see:

    http://wiki.processing.org/w/From_several_arrays_to_classes

    and #6 here: Objects

    https://forum.processing.org/tutorials/

    Chrisir ;-)

  • How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it

    • mark the code (without the 2 empty lines)

    • press C in the small command bar.

  • Hi Chrisir.

    Thanks for the help on this one, its helped me alot.

    Also sorry about the poor set-up of the question. I've only just started using this forum, so thanks for the advice on posting future questions.

    Thanks again.

    Gragnit

  • the forum is broken somehow anyway....

Sign In or Register to comment.