hey guys, I have a very short piece of code which I am trying to write, but it seems that the translate function is causing some sort of error. anyone know why?
function setup() {
cnv = createCanvas(400, 400, WEBGL);
buffer = createGraphics(width, height, WEBGL)
cube = new Cube(5, 50, 255, 0,0,0)
cam = createEasyCam();
}
function draw() {
background(220);
cube.show()
}
function mouseClicked(){
console.log(buffer.get(mouseX, mouseY));
}
class Cube{
constructor(id, size, c, x, y, z){
this.id = id;
this.size = size;
this.c = c;
this.x = x;
this.y = y;
this.z = z;
}
show(){
push()
translate(this.x, this,y, this.z)
fill(this.c)
box(this.size)
buffer.fill(this.id)
buffer.box(this.size)
translate(-this.x, -this.y, -this.z)
pop()
}
}
computers are stupid (or clever). itâs like being in a relationshipâŚ
they only love you when you say the âexactâ words they expect from you. otherwise they pout and refuse to even talk to you or do what you ask themâŚ
simply use ;
after every statement.
you will see, your code will say
1 Like
no, doesnât work
anyway, p5 is a pretty loosely typed language so there can be some exceptions.
Even when added, it does the same error
in how many places did you add the ; ?
i see its missing in 11 places
I did all of them.
For some reason it didnât work. I think it is for another reason which I donât know.
here is a little list:
- I think you should still declare your variables outside setup():
Reference:
Variables that are declared with let will have block-scope. This means that the variable only exists within the block that it is created within.
let buffer;
let cube;
-
p5js may be sloppy, but im sure not mixing the way you formulate things is a good idea, so ending a code line with ; is good
-
im not sure why you want to use a variable for createCanvas â âcnvâ = createCanvasâŚ
-
you seem to have a typo at: translate(this.x, this, y, this.z);
should be âthis.yâ. (thatâs where youâre code actually stops in my tests)
-
you do not have to translate things âbackâ. that is what push() and pop() is actually for.
so no need to say: translate(-this.x,âŚ)
-
if you draw into a PGraphics, you need to display that too, e.g. in draw()
image(buffer,-width/2,-height/2);
So, actually, only items 4 and 6 on my list might be considered errors in your code
But maybe the others are interesting to you tooâŚ
it was just the this.y :((((((((
i am sad now.
thanks tho @Hyperion65