Dimentio
Dimentio

Reputation: 21

Type Coercion failed: cannot convert []@13a355b9 to flash.display.DisplayObject

I get this error when running, no compile errors.

package  {
import flash.display.*;
import flash.events.*;
import flash.utils.*;

public class mainCode extends MovieClip{
    //global variables go here
    public var circled:Shape = new Shape();
    public var circled2:Shape = new Shape();
    public var circled3:Shape = new Shape();
    public var angled:int = new int();
    public var circlearray1:Array = new Array(4); 
    public var circlearray2:Array = new Array(4); 
    public function mainCode(){
        makeCircle(circled, 100);
        makeCircle(circled2, 50);
        makeCircle(circled3, 50);
        for(var i:int=0; i<4; i++){circlearray1[i] = new Array(20);}
        for(var n:int=0; n<4; n++){circlearray2[n] = new Array(20);}
        stage.addEventListener(Event.ENTER_FRAME, mainLoop);

    }
    //functions go here
    private function mainLoop(e:Event){
        trace(1);
        angled+=1;
        angled%=360;
        circled.x = stage.stageWidth / 2;
        circled.y = stage.stageHeight/ 2;
        circled2.x = circled.x + 100*Math.cos(radians(angled));
        circled2.y = circled.y + 100*Math.sin(radians(angled));
        circled3.x = circled.x + 100*Math.cos(radians((angled + 180) % 360));
        circled3.y = circled.y + 100*Math.sin(radians((angled + 180) % 360));
        trace(2);
        for (var i:int = 0; i < 4; i++)
        {
            trace(3);
            if (circlearray1[i][0] != undefined) 
            {removeChild(circlearray1[i][0]);}
            if (circlearray2[i][0] != undefined) 
            {removeChild(circlearray2[i][0]);}
            trace(4);
            for (var m:int = 0; m < 19; m++)
            {
                circlearray1[i][m] = circlearray1[m+1];
                circlearray2[i][m] = circlearray2[m+1];
            }
            circlearray1[i][19] = new Shape();
            circlearray2[i][19] = new Shape();
            makeCircle(circlearray1[i][19], 25);
            makeCircle(circlearray2[i][19], 25);
            circlearray1[i][19].x = circled2.x + 50*Math.cos(radians(((-angled + (i*90)) * 2) % 360));
            circlearray1[i][19].y = circled2.y + 50*Math.sin(radians(((-angled + (i*90)) * 2) % 360));
            circlearray2[i][19].x = circled3.x + 50*Math.cos(radians(((-angled + (i*90)) * 2) % 360));
            circlearray2[i][19].y = circled3.y + 50*Math.sin(radians(((-angled + (i*90)) * 2) % 360));
            trace(8);
        }
    }
    private function makeCircle(circles:Shape, radius:int)
    {
        circles.graphics.clear();
        circles.graphics.lineStyle(2,0x000000);
        circles.graphics.beginFill(0x990000);
        circles.graphics.drawCircle(0,0,radius);
        circles.graphics.endFill();
        addChild(circles);
    }
    private function degrees(radians:Number):Number
    {
        return radians * 180/Math.PI;
    }

    private function radians(degrees:Number):Number
    {
        return degrees * Math.PI / 180;
    }
    private function killCircle(circlei:Shape):void {
        removeChild(circlei);
    }
}   
}

I've traced it down to {removeChild(circlearray1[i][0]);}, which seems to be returning the error. I have no idea why it's doing this, I've tried alternatives such as circlearray1[i][0].parent.removeChild(circlearray1[i][0]); and if (circlearray1[i][0] is Shape) ... , but no dice.

For reference, I'm trying to make some circles circle around other circles, but have the outermost circles leave an "image lag" (or afterimage) while moving. I do this by creating objects and deleting them using for loops and multidimensional arrays, as I dont feel like typing out 50 create object calls manually and edit each one manually.

Upvotes: 0

Views: 49

Answers (1)

Organis
Organis

Reputation: 7316

Probably, this part:

        for (var m:int = 0; m < 19; m++)
        {
            circlearray1[i][m] = circlearray1[m+1];
            circlearray2[i][m] = circlearray2[m+1];
        }

You assign to endpoint elements, which you initially assume to be Shapes the elements which are Arrays. Then later you go at circlearray1[i][0] assuming it is Shape if it is not empty, but it is already an Array due to the assignment above. It's probably a typo and you meant this:

        for (var m:int = 0; m < 19; m++)
        {
            circlearray1[i][m] = circlearray1[i][m+1];
            circlearray2[i][m] = circlearray2[i][m+1];
        }

There's actually a more clean way to do that. Array in AS3 is a lot like C# ArrayList, you can insert elements to either end and extract elements from either end without re-indexing elements manually:

        trace(3);
        // If you are sure the first element is not empty
        // you can get its reference removing them from 
        // the Array at the same time.
        removeChild(circlearray1[i].shift());
        removeChild(circlearray2[i].shift());
        trace(4);
        // All Array elements are automatically shifted
        // by -1 so you don't need to move each of them manually.
        circlearray1[i][19] = new Shape();
        circlearray2[i][19] = new Shape();

Upvotes: 1

Related Questions