Type 1061: Call to a possibly undefined method addChild through a reference with static type slotmachine:SlotMachineGame

StackOverflow https://stackoverflow.com/questions/14989550

  •  10-03-2022
  •  | 
  •  

Question

Im having issues with this part of the code, it's supposed to add the pieces into an array, but I keep getting this error. This is the part of the code:

    function addPiece(xCoord:Number,yCoord:Number,id:Number){
        classLoader = getDefinitionByName("piece"+id) as Class;
        var tmp:Piece = new classLoader();
        tmp.x = xOffSet+(xCoord * 128);
        tmp.y = yOffSet+(yCoord * 128);
        tmp.id = id;
        //trace("===>"+tmp.x + ","+tmp.y)
        pieces.push(tmp);
        this.addChild(tmp);
    }

Thank you very much for your help.

Was it helpful?

Solution

You are calling a method on the class, not the instance of the class, calling addPiece() from a static method where this is not available at the current scope.

Add pieces to an instance of your slot machine game class:

var slotMachine:SlotMachine = new SlotMachine();
slotMachine.addPiece(0, 0, 1);

Otherwise if you call from a static method, this.addChild() does not exist in the current context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top