We are about to switch to a new forum software. Until then we have removed the registration on this forum.
okay. So , its simple. I can´t find reference of how to create an object in instance mode.
Is it possible ? How is it doned?
This is my code and what ive tryed so far :
function Ball(x,y) {
this.x = x;
this.y = y;
this.xspeed = 1;
this.yspeed = 1;
this.display = function(){
stroke(20);
strokeWeight(1);
noFill();
ellipse(this.x,this.y,20,20);
}
this.move = function() {
this.x = this.x +this.xspeed;
this.y = this.y + this.yspeed;
}
this.bounce = function () {
if (this.x > width || this.x < 0){
this.xspeed = this.xspeed * -1;
}
if (this.y > height || this.y < 0){
this.yspeed = this.yspeed * -1;
}
}
}
//CANVAS 2
var sketch2 = function(can2){
can2.Paints = [] ;
can2.i;
can2.a = 50;
can2.Ball(30,30) ;
can2.setup = function () {
can2.createP ('Canvas2');
can2.createCanvas(300, 300) ;
};
can2.draw = function () {
can2.background('#F0F');
can2.bebito.display();
};
};
new p5(sketch2, 'canv2');
Answers
Given that your function class Ball was defined outside your sketch function, obviously the former can't access the latter's parameter can2.
Either place Ball inside sketch2 or request can2 from its constructor.
This forum thread got some examples:
https://forum.Processing.org/two/discussion/16496/instance-mode-namespace
You can also experiment them online:
http://CodePen.io/anon/pen/mPvpOq?editors=1010
http://CodePen.io/anon/pen/YWWRaO?editors=0010