#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to Randomly Spawn Shapes with Classes

edited May 2016

Okay, so for my intro to programming class I am trying to make a drag and drop sort of simulation and while I have the dragging concept ready, my issue is how to randomly spawn the objects among the two inherited classes. I tried to just use random() in the ellipse() drawing function which proved no good so I am unsure how to make them so they spawn in a different place over a (500,500) area.

Here are screenshots of the current code. I wasn't sure how to make it appear here and thought this would be easier.

Any help would be appreciated.

Tagged:

## Answers

• I wasn't sure how to make it appear here and thought this would be easier.

I'll give you my super secret keyboard shortcut.You can try it to copy your code (it works on other stuff too!!) : Ctrl+C and then to paste it here Ctrl+v

• edited November 2014

How exactly random didn't work? Try:

`````` randomX = random(width);
randomY = random(height);
``````

And what's the point to create x,y,w and h variables if you do not use them in draw dunction of your Ellipse class? Maybe you want to assign random position when you will be creating an instance of a class in the main body of your program? Something like:

``````randomX = random(width);
randomY = random(height);
el = new Ellipse(randomX, randomY, 60, 60);
``````

then in your Ellipse.draw() place: `ellipse(x,y,w,h);`

• @shantee, besides those "secret" shortcuts, once pasted here, we gotta highlight it and hit CTRL+K in order to format it w/ 4-space indentation! 3:-O

• @Ater It's because the shapes will be dragged and moved into another position but when they first appear, they would be in a random spot across the canvas.

Sign In or Register to comment.