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
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>