0

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???
                }
           }
xtreme
  • 11
  • 2
  • 2
    If you make `moveitman` an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), then you'll be able to access the correct `this` value – Nick Parsons May 02 '22 at 06:24

0 Answers0