Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"UPDATE" could not be called as a function

this is the update function

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

this is where it is called

function draw() {
    background(0);
    rocket.UPDATE();
    rocket.render();
}

the error in the non-p5.js console says that UPDATE is not a function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is the whole code

function Rocket() {
    this.pos = createVector(width/2, height);
    this.vel = createVector(0, -1);
    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.render = function(){
    push();
    translate(this.pos.x, this.pos.y);
    rotate(this.vel.heading()); 
    rectMode(CENTER);
    Rect(0,0, 50, 10);
    Pop();
}

function setup() {
    createCanvas(400, 300)
    rocket = new Rocket();
}
function draw() {
    background(0);
    rocket.UPDATE();
    rocket.render();
}

i was expecting a rectangle to start moving upwards from the bottom of the screen. i am following a coding challenge by the coding train to do this.

>Solution :

UPDATE and render must be set in the scope of Rocket, but not in the global scope:

function Rocket() {
    this.pos = createVector(width/2, height);
    this.vel = createVector(0, -1);
    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.render = function(){
        push();
        translate(this.pos.x, this.pos.y);
        rotate(this.vel.heading()); 
        rectMode(CENTER);
        rect(0,0, 50, 10);
        pop();
    }
}

function setup() {
    createCanvas(400, 300)
    rocket = new Rocket();
}
function draw() {
    background(0);
    rocket.UPDATE();
    rocket.render();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.min.js"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading