Calling object methods in Javascript

Zach R

I have practically no experience in JS (I mostly program in C#). I'm trying to create a random layout of circles (stars) in an html5 canvas and I have them moving at a random x and y velocity. I created a Star class to make many of these objects and manipulate them, but when I call the Update() method, I receive this TypeError:

TypeError: Cannot read property 'Update' of undefined at animate (file:///F:/Code/Website/main.js:67:20) at file:///F:/Code/Website/main.js:71:1

Here is the code that is throwing the error:

//Circle object
class Star 
{
    constructor(X, Y, VX, VY, R) 
    {
        this.x = X;
        this.y = Y;
        this.vx = VX;
        this.vy = VY;
        this.r = R;
    }

draw() {
    c.beginPath();
    c.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
    c.strokeStyle = "blue";
    c.stroke();
};

Update() {
    if (this.x + this.r > pageWidth || this.x - this.r < 0) {
        this.vx = -this.vx;
    }

    if (this.y + this.r > pageHeight || this.y - this.r < 0) {
        this.vy = -this.vy;
    }

    //add velocity
    this.x += this.vx;
    this.y += this.vy;

    this.draw();
};


}

for (var i = 0; i < 50; i++) {
    var x = Math.random() * canvas.width;
    var y = Math.random() * canvas.height;
    var vx = Math.random();
    var vy = Math.random();
    var radius = 30;
    let star = new Star(x, y, vx, vy, radius);
    starArr.push(star);
}
console.log(starArr);

function animate() {
    "use strict";
    window.requestAnimationFrame(animate);

    c.clearRect(0, 0, pageWidth, pageHeight);

    for (var j = 0; j < starArr.length; j++); {
        starArr[j].Update();
    }
}

animate();
enxaneta

You have a typo at the for loop that calls the Update method: an extra semicolon ; for (var j = 0; j < starArr.length; j++); {

const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d");
let pageWidth = canvas.width = window.innerWidth;
let pageHeight = canvas.height = window.innerHeight;
let starArr = []

//Circle object
class Star{
constructor(X, Y, VX, VY, R){
        this.x = X;
        this.y = Y;
        this.vx = VX;
        this.vy = VY;
        this.r = R;
    }

draw() {
    c.beginPath();
    c.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
    c.strokeStyle = "blue";
    c.stroke();
};

Update() {
    if (this.x + this.r > pageWidth || this.x - this.r < 0) {
        this.vx = -this.vx;
    }

    if (this.y + this.r > pageHeight || this.y - this.r < 0) {
        this.vy = -this.vy;
    }

    //add velocity
    this.x += this.vx;
    this.y += this.vy;

    this.draw();
};


}

for (var i = 0; i < 50; i++) {
    var x = Math.random() * canvas.width;
    var y = Math.random() * canvas.height;
    var vx = Math.random();
    var vy = Math.random();
    var radius = 30;
    let star = new Star(x, y, vx, vy, radius);
    starArr.push(star);
}


function animate() {
    //"use strict";
    window.requestAnimationFrame(animate);

    c.clearRect(0, 0, pageWidth, pageHeight);

    for (var j = 0; j < starArr.length; j++) {
        starArr[j].Update();
    }
}

animate();
<canvas></canvas>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript: calling methods in classes with this

From Dev

Calling multiple methods of an object in sequence

From Dev

Javascript registry for calling methods and objects

From Java

calling java methods in javascript code

From Dev

Calling JavaScript methods from typescript

From Java

Calling methods on reference variable vs Calling methods on a new object

From Dev

Intercepting Methods of an object in javascript

From Dev

Javascript - usage of this in object methods

From Dev

Clojure: calling a sequence of methods on a Java object

From Java

Calling object methods within Arrays.reduce(...)

From Java

Shortcut for calling all the setter methods on an object in Eclipse?

From Dev

Is there any Design Pattern for calling object methods statically

From Dev

Calling external methods without instantiating an object

From Dev

Best practice when calling self object methods

From Dev

Return type annotation for calling an object's methods

From Dev

Calling methods of actual object instead of parent class

From Dev

calling methods using object reference variable

From Dev

Calling multiple methods for one object simultaneously?

From Dev

Calling class methods inside javascript class

From Dev

Basic Object Structure and Calling for JavaScript

From Dev

JavaScript calling a method on an object syntax

From Dev

Javascript calling other object method

From Dev

Casting an object in an Array vs ArrayList and calling methods on the object

From Dev

Getting object fields and calling object functions in JavaScript

From Dev

JavaScript Object and methods but getName is not a function

From Dev

JavaScript methods on object not updating property

From Dev

Strip methods off javascript object

From Dev

new constructors in javascript object methods

From Dev

Object methods in JavaScript with bracket notation

Related Related

  1. 1

    Javascript: calling methods in classes with this

  2. 2

    Calling multiple methods of an object in sequence

  3. 3

    Javascript registry for calling methods and objects

  4. 4

    calling java methods in javascript code

  5. 5

    Calling JavaScript methods from typescript

  6. 6

    Calling methods on reference variable vs Calling methods on a new object

  7. 7

    Intercepting Methods of an object in javascript

  8. 8

    Javascript - usage of this in object methods

  9. 9

    Clojure: calling a sequence of methods on a Java object

  10. 10

    Calling object methods within Arrays.reduce(...)

  11. 11

    Shortcut for calling all the setter methods on an object in Eclipse?

  12. 12

    Is there any Design Pattern for calling object methods statically

  13. 13

    Calling external methods without instantiating an object

  14. 14

    Best practice when calling self object methods

  15. 15

    Return type annotation for calling an object's methods

  16. 16

    Calling methods of actual object instead of parent class

  17. 17

    calling methods using object reference variable

  18. 18

    Calling multiple methods for one object simultaneously?

  19. 19

    Calling class methods inside javascript class

  20. 20

    Basic Object Structure and Calling for JavaScript

  21. 21

    JavaScript calling a method on an object syntax

  22. 22

    Javascript calling other object method

  23. 23

    Casting an object in an Array vs ArrayList and calling methods on the object

  24. 24

    Getting object fields and calling object functions in JavaScript

  25. 25

    JavaScript Object and methods but getName is not a function

  26. 26

    JavaScript methods on object not updating property

  27. 27

    Strip methods off javascript object

  28. 28

    new constructors in javascript object methods

  29. 29

    Object methods in JavaScript with bracket notation

HotTag

Archive