spawning random objects

edited November 2013 in How To...

hello, i have been trying to spawn ellipse below the y axis on processing but i do not know how to use "random" this is what i have, can someone help/correct my work?

void draw() {
 background(255);
  if(random(0,100)<10){
    fill(229,28,35); //Red balloon
  ellipse(width/2,530-a,30,30);
  a= a- 1;
  }

Answers

  • Answer ✓

    I have a problem of vocabulary. How do you spawn one ellipse? Do you mean to draw it multiple times? What do you call y axis, and how do you go below it?

    random() allows to generate a random number between two bounds, you can use it to generate random coordinates in a given range. You can also use a for loop to draw several ellipses, if that's what you want to do.

  • edited November 2013

    The size of my project is size(500,500) I am trying to spawn an ellipse with the coordinates out of the screen, below the y axis...ex:

    int a= 5
    ellipse(width/2,530-a,30,30); //this makes the ellipse rise(on the y axis)
    a= a- 1;
    

    I want to make the ellipse spawn at random below the y axis, and anywhere on the x axis.

  • use another variable (x?) for the first argument of ellipse().

    set it to a random number between, roughly, 0 and width. see random() in the reference.

  • edited November 2013

    ok thanks i was able to set up random spawn now. What would i have to set up for the ellipse to be deleted if it's clicked on when the mouse is over the ellipse?

  • Answer ✓

    Ah, OK. The Y axis is the vertical one, so I suppose you meant beyond the highest Y value. And spawning was in the sense of rising, appearing gradually.

    To detect a mouse click within the ellipse (a circle, actually), you have to create the mouseClicked() function, and there, to check if the dist() between mouseX / mouseY coordinates and the center of the circle are below the radius of the circle.

  • edited November 2013

    I set up my mouseClicked but it is not working.

    void mouseClicked() {
      if (mouse) {
        ((mouseX && mouseY-7) == (x && 530+a));
      }
    

    the second coordinates "x" and "530=a" represent the ellipse, what did i do wrong?

  • Answer ✓

    "what did i do wrong?"
    Well, I fear that creating syntax isn't accepted by Processing... :-)

    Again, try and explore a bit the examples in Processing, to get a feel how things work. There are a number of examples (File > Examples...) that handle mouse click on a circle.

    Hint: you need the if keyword, and as said, the dist() function.

Sign In or Register to comment.