PDA

View Full Version : Type Coercion failed?



Cutless009
September 10th, 2009, 09:37 AM
Not sure what this error means, but it looks like this:

"Type Coercion failed: cannot convert *object* to *movieClip*"

What Im trying to do is create a new Layer (swc asset, is a movieclip), inside a previous created Layer, and draw on it instead of on the original Layer.


any help? The end goal here is I have squares, circles, and lines, and if I paint a square or circle shape (which are classes, and are added to the canvas using addChild, I cannot change this), I need the lines to paint on top of them if I go back to using the line tool.



//-- Layers --\\
private var layer:Layer;
private var layers:Array;
private var numLayers:int;
private var currentLayer:int;
private var overLayer:MovieClip;


//-- Make Layer --\\
layer = new Layer();
layer.graphics.lineTo(1,1);
canvas.addChild(layer);
layer.currentOverlay = 0;
layers.push(layer);

//-- Set Variables --\\
numLayers ++;
currentLayer = layers.length - 1;

//-- Make Layer Selector --\\
layerBox = new LayerBox;
layerBox.tfLayerBox.text = "Layer " + currentLayer;
layerBox.mouseChildren = false;
layerBox.buttonMode = true;
layerBox.addEventListener(MouseEvent.CLICK, setLayer);
layerBox.addEventListener(MouseEvent.CLICK, editLayer);
layerBox.index = currentLayer;
if (currentLayer == 0)
{
layerBox.tfLayerBox.text = "Main Layer"
}
layerBox.x = 0;
layerBox.y = (-layerBox.height * (numLayers)) + layerCont.height - layerBox.height + 20;
layerCont.addChild(layerBox);
layerBoxes.push(layerBox);


private function startPainting(event:MouseEvent):void
{
canvas.addEventListener(Event.ENTER_FRAME, paint);
canvas.addEventListener(MouseEvent.MOUSE_UP, stopPainting);
layers[currentLayer].graphics.moveTo(canvas.mouseX, canvas.mouseY);

if (layers[currentLayer].numChildren > 1)
{
var overLayer:Layer = new Layer;
layers[currentLayer].addChild(overLayer);
layers[currentLayer].currentOverlay ++;
overLayer = layers[currentLayer].getChildAt(layers[currentLayer].currentOverlay);
}
}

private function paint(event:Event):void
{
var ct:ColorTransform = new ColorTransform;
ct.color = drawColor;

if (drawMode == "line")
{
if(layers[currentLayer].numChildren > 1)
{
overLayer.graphics.lineStyle(drawSize, drawColor, drawAlpha);
overLayer.graphics.lineTo(canvas.mouseX, canvas.mouseY);
}
layers[currentLayer].graphics.lineStyle(drawSize, drawColor, drawAlpha);
layers[currentLayer].graphics.lineTo(canvas.mouseX, canvas.mouseY);
}

IQAndreas
September 10th, 2009, 10:11 AM
Make sure that Layer extends MovieClip, not just has the same properties. But you probably already have this, only Flash likely doesn't know that.

Perhaps you can change this line:

private var overLayer:MovieClip;
//to
private var overLayer:Layer;

Otherwise, how does the code look for "Layer", and how are you loading it into your application?

Also, when testing the application, run it using the Flash debugger (with command "CTRL+ALT+SHIFT"). That way you can know exactly what line is causing the error. Please post as much error information as possible whenever possible. (copy and paste the error message)

kounoupi
September 10th, 2009, 10:33 AM
Also, when testing the application, run it using the Flash debugger (with command "CTRL+ALT+SHIFT"). That way you can know exactly what line is causing the error. Please post as much error information as possible whenever possible. (copy and paste the error message)

Instead of the debugger, an easier way to get line numbers for error messages is to go to File->Publish Settings->Flash and select the "Permit debugging" checkbox. It increases the file size a bit, but during development it's a godsend!

Cutless009
September 10th, 2009, 10:38 AM
The exact error is:

"TypeError: Error #1034: Type Coercion failed: cannot convert Square@30fdc191 to libs.Layer.
at DrawApp/startPainting()[/Users/stephenrios/Documents/Flex Builder 3/DrawingAPI/src/DrawApp.as:454]"

line 454 is as follows:

overLayer = layers[currentLayer].getChildAt(layers[currentLayer].currentOverlay);

kounoupi
September 10th, 2009, 10:46 AM
The exact error is:

"TypeError: Error #1034: Type Coercion failed: cannot convert Square@30fdc191 to libs.Layer.
at DrawApp/startPainting()[/Users/stephenrios/Documents/Flex Builder 3/DrawingAPI/src/DrawApp.as:454]"

line 454 is as follows:

overLayer = layers[currentLayer].getChildAt(layers[currentLayer].currentOverlay);

The error message means that you cannot convert from the Square class (currentOverlay) to the Layer class (overLayer), ie Layer is not a superclass of Square.

Cutless009
September 10th, 2009, 11:23 AM
why is it trying to convert square to a layer?

Cutless009
September 10th, 2009, 11:36 AM
Grr, nm, I was pulling the wrong child, but now it says a null object or reference, but it's a Sprite that's being targeted.

Here is the error:

[object Sprite]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DrawApp/paint()[/Users/stephenrios/Documents/Flex Builder 3/DrawingAPI/src/DrawApp.as:466]

the [object Sprite] was a trace of the exact target.

line 466 is as follows:

overLayer.graphics.lineStyle(drawSize, drawColor, drawAlpha);

simply trying to paint on that layer.
overLayer was set here:

overLayer = layers[currentLayer].getChildAt(layers[currentLayer].numChildren - 1);

or simply, get the last child in the layer, since just before that we added the new Sprite to its display list.

IQAndreas
September 10th, 2009, 01:00 PM
Trace all of these items, and if any one of them traces out null, there is your culprit:

trace(overLayer);
trace(overLayer.graphics);
trace(drawSize);
trace(drawColor);
trace(drawAlpha);

Cutless009
September 10th, 2009, 01:44 PM
Thanks IQA, I got it fixed, I accidently re-created my overLayer variable in the function that created it, so it was trashing it after that function ran, hence null reference. took the var overLayer = out and just put var = and it was fixed :) And now it works!! YAYY!!

*/end nub questions.... for now...*