We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to create mousePressed ellipses from and array in P5, but I'm having trouble defining my objects. I've just transitioned from processing to P5 and I'm a little lost. Here is my code:
var Shape;
var value = 0;
var value2 = 0;
var x;
var y;
function setup() {
colorMode(HSB, width, height, 100);
createCanvas(500, 500);
Shape = new ball();
}
function mousePressed(){
//If mouse is pressed outside sliding bar, add a shape.
if (mouseY > (height/2 + 40) || ( mouseY < ( height/2 - 20))){
for(var i = 0; i < 1; i++){
balls.add(new Shape(mouseX, mouseY));
if (balls > 8) {
balls.remove(0);
println(balls());
}
}}
}
function draw() {
background(51);
for (var i = Shape -1; i >= 0; i--) {
// ball[i].move();
ball[i].display();
if(balls[i].isOffScreen()){
balls.remove[i];
}
}
}
function ball() {
//An array for all the shapes
this.balls = []; //initialize the array
this.x = x;
this.y = y;
this.display = function() {
// stroke (255);
fill(200, 252, 70);
ellipse(this.x, this.y, 50, 50);
}
}
Answers
A lot of your syntax doesn't make any sense.
First of all, why does your
Ball
class need aballs
array? Shouldn't an instance ofBall
be a single ball? Get rid of that.Secondly, your
Ball
constructor doesn't take any arguments, but you're usingx
andy
arguments. If you want to use them, you have to pass them into the constructor.Third, what are you doing with your sketch-level variables at the top? What are
x
andy
meant for? Why do you only have one shape? Don't you want yourballs
array to be up here?Fourth, you're using
ball
array outside theBall
object, and you're calling aballs.add()
which isn't a function. Maybe you want theappend()
function?Here's a little example that puts it all together:
On a more general note, you've got a ton of syntax and logic problems. This is usually caused by trying to write the entire program at one time. Instead, try to break your problem down into smaller steps: can you write a little example program that shows a single ball? Get that working first, that way you know your syntax and constructor work okay. Then can you create a separate example program that draws an array of
PVector
instances? Get that working so you know your array syntax works okay. Then you can work your way up and get more complicated, use an array of your own objects, etc. Trying to write your entire project at one time will just give you headaches.Thanks Kevin, I thought I had simplified it to a small chunk. So now I have an add on question though. I want to remove balls. So if balls > 8 I want to remove the oldest ball and add a new ball. I can't find an equivalent to remove, I'm trying shorten, but it doesn't do. I'm wondering perhaps its my if statement?
For questions like these, you should always consult the reference. That will show you that the syntax is actually this:
In other words, the
shorten()
function returns a different array that contains one less element than the input array. It doesn't change the input array.So if you wanted to remove one from an array, you'd have to set the variable to the new array returned by the
shorten()
function:Also note that this removes the last index of the array. With your code, that will be the Ball you just added, which is the opposite of what you want. You either need to rearrange your array so the oldest ball is at the end of the array (the
splice()
function might help with this), or you need to remove the Ball from the front of the array (thesubset()
function might help with this).As always, your best friend is always the reference. Check out the Array Functions section in particular.
thanks Kevin, but the references section of P5 is vague and doesn't explain much I need code examples.
All of the pages in the reference contain code examples.
If you have a specific question, feel free to post an MCVE, and we'll go from there.
Alright I'm trying the subset here in mousePressed I thought it matches the reference, but it doesn't do anything.
http://p5js.SketchPad.cc/sp/pad/view/ro.CxS6E9WWAMC$EO/latest
Thank you kindly