• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

253
Views
Undefined: How do I reference this class method within a class properly?

I cant call the Circle class 'move' function from a function within a function

I have tried a couple of fixes like making functions that run the function I want, but that just resulted in the same problem.

I simplified things to make it easier to read

class Bullet
    {
        constructor()
        {
            this.bullet = new Circle(radius);
            // Circle class contains a function move(x,y);
        }

Calling this works tho

        setpos(x,y){
            this.bullet.move(x,y)
        }

This too

        moves(xmov, ymov, moveTime)
        {
            this.bullet.move(xmov,ymov);
            setpos(xmov,ymov); 
        }

this is fully working, but not what I want.

this version is what i think should work, but function is "undefined":

        moves(xmov, ymov, moveTime)
        {
            this.xmov = xmov; // assigning parameters to instance of object hopefully correctly
            this.ymov = ymov;
            this.moveTime = moveTime;
            
            // probably unnecessary but it didnt recognise it otherwise?
            var mt = this.moveTime;
            var xmv = this.xmov;
            var ymv = this.ymov;

            var timerCount = 0;

            setTimer(moveitman,1) //  <-- calls moveitman() every 1ms
            function moveitman() {

                timeCount++;

                if (timerCount < mt) {
                    var xSpeed = xmv / mt; //  < this stuff works
                    var ySpeed = ymv / mt;
                    
                    this.bullet.move(xSpeed,ySpeed); // undefined but worked outside the function???
                }
           }
about 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error