samedi 20 février 2016

AS3: Flash throwing Type Error #1006 value is not a function for what seems to be no reason; is this a bug?

Flash seems to be screwing with me and is throwing this error:

TypeError: Error #1006: value is not a function.
    at sweg::Force/setTxt()[C:\Users\lamide97\Google Drive\OneDrive\COMP4\sweg\Force.as:459]
    at Main/CheckOnStage()[C:\Users\lamide97\Google Drive\OneDrive\COMP4\Main.as:248]
    at sweg::Force/removed()[C:\Users\lamide97\Google Drive\OneDrive\COMP4\sweg\Force.as:192]
    at flash.display::DisplayObjectContainer/removeChild()
    at Main/init()[C:\Users\lamide97\Google Drive\OneDrive\COMP4\Main.as:310]
    at Main/Start()[C:\Users\lamide97\Google Drive\OneDrive\COMP4\Main.as:107]

for what seems to be no reason. I looked around at several other #1006 TypeError questions but they didn't have the same problem as mine. So I was resolved to fix it myself. Surprisingly it didn't take very long; the solution was a single trace statement. But I'm stumped as to why simply tracing a value solved it.

Here's the premise: I have a Force class that once added to stage will set the text in a textbox on stage with it's magnitude property. The textbox's text it assigns is respective to the Force's name.

Here's the code:

public function setTxt():void
    {
        switch (this.name)
        {
            case "Force 1" :
                _magtText.text = "P";
                _forceFormat.color = 0x33CCFF
                //trace(_magt); If I uncomment this line then everything works
                (this.parent as MovieClip).force1.text = String(Math.round(_magt * 100) / 100);
                //line above is throwing error.
                break;

            case "Force 2" :
                _magtText.text = "Q";
                _forceFormat.color = 0xFF6600;
                (this.parent as MovieClip).force2.text = String(Math.round(_magt * 100) / 100);
                break;

            case "Force 3" :
                _magtText.text = "F";
                _forceFormat.color = 0x003300;
                (this.parent as MovieClip).force3.text = String(Math.round(_magt * 100) / 100);
                break;

            case "Force 4" :
                _magtText.text = "A";
                _forceFormat.color = 0xFF00FF;
                (this.parent as MovieClip).force4.text = String(Math.round(_magt * 100) / 100);
                break;

            case "Tension 1" :
            case "Tension" :
                _magtText.text = "T";
                _forceFormat.color = 0x003300;
                (this.parent as MovieClip).force3.text = String(Math.round(_magt * 100) / 100);
                break;

            case "Tension 2" :
                _magtText.text = "T`";
                _forceFormat.color = 0xFF00FF;
                (this.parent as MovieClip).force4.text = String(Math.round(_magt * 100) / 100);
                break;

            case "Resultant F" :
                _magtText.text = "R";
                _forceFormat.color = 0xFF0000;
                (this.parent as MovieClip).resBox.text = String(Math.round(_magt * 100) / 100);
                (this.parent as MovieClip).resAng.text = roundNum(_degrees.toFixed(3));
                break;

            default :
                _magtText.text = "X";
        }
        _magtText.setTextFormat(_forceFormat);
    }

I suspect it has something to do with the _magt property, because I encountered a similar problem in my Weight subclass that inherits Force prior to this, and the problem seemed to solve itself after I appended some semicolons.

if (_magt >= 0)
    {
        drawForce();
    }
    else
    {
        drawForce(0x33CCFF, 0xCCCCCC, 0.3);
    }

    (this.parent as MovieClip).weightBox.text = roundNum(_magt.toFixed(3))
    (this.parent as MovieClip).particle.mass = _magt;
    //Line above throws a "Call to a possibly undefined method _magt" error. Doesn't work without the semi-colon.
    (this.parent as MovieClip).massNum.text = roundNum(Number(_magt / 9.8).toFixed(3))
    (this.parent as MovieClip).weightSlider.value = _magt;
    //Line above throws the same error; also doesn't work without semi colon.
    (this.parent as MovieClip).massSlider.value = _magt / 9.8

    (this.parent as MovieClip).dragCalc(this);

I don't know why flash would seem to think that the _magt property, which I declared as Number, is a method. I do however have a get magt function but to my knowledge I don't think Flash would confuse the function and property with each other since they don't share a namespace.

What's even more strange is that when I have a force named "Resultant F" it doesn't seem have problems with its _magt but all the other ones do.

My initial guess was that at some point somewhere, _magt was NaN. But I knew that couldn't be possible because the force is instantiated with a _magt of 147, which is what the traced magnitude displayed, and it isn't assigned a new value anywhere else.

I don't know if this is some low-key syntax error, as I'm still a beginner and there may be something hugely obvious that I'm not seeing.

Although technically I've already solved this problem, I'd like to know what's causing it because I have a feeling it might creep up on me later on.

Thank you for reading.

Aucun commentaire:

Enregistrer un commentaire