why wont my program draw my rectangle?

83 Views Asked by At

i have a set of code that i copied from someone on youtube and i completely understand and there is some unused logic in there but basically at the moment all it is meant to do is grab the rocket.show function and draw a rectangle and i cannot for the life of me work out why it is not doing so. it is throwing out no errors and drawing the background, i can draw the rectangle if i just put the code for "rect..." in the draw function therefore there is a problem with the way that i am referring to the function but i cannot for the life of me work it out, the code is below, any help would be appreciated.

function setup() {
    createCanvas(800, 600);
    background(0);
    rocket = new rocket();
}

function draw() {
    rocket.update;
    rocket.show;
}

function rocket() {

    this.pos = createVector();
    this.vel = createVector();
    this.acc = createVector();

    this.applyforce = function (force) {
        this.acc.add(force);
    }

    this.update = function () {
        this.vel.add(this.acc);
        this.pos.add(this.vel);
        this.acc.mult(0);
    }

    this.show = function () {
        push();
        translate(this.pos.x, this.pos.y);
        rotate(this.vel.heading());
        rectMode(CENTER);
        rect(0, 0, 10, 50);
        pop();
    }
}

edit: I worked it out, sorry for taking up needless posting space

2

There are 2 best solutions below

0
On BEST ANSWER

You're not properly calling your functions.

In javascript, functions are variables, so I can say a = Math.floor, and then I can call a(1.76) and get 1 as a response. This means however, that whenever you call a function, you have to include parameters even if you don't use any. You do this with an empty set of parentheses. If you don't include these parentheses, what you're doing is essentially calling console.log(whatever function).

In your case, this means you need to replace rocket.update and rocket.show with rocket.update() and rocket.show()

3
On

After rocket.update it needs to look like this: rocket.update();. My bad.