PDA

View Full Version : Calling addChild on canvas to add Bitmap giving runtime error


Ninja_Warrior
01-19-2009, 06:46 PM
HI All, I am trying to add a Bitmap object to a canvas , but during runtime I am receiving error ...

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@1691581 to mx.core.IUIComponent.
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\Container.as:3251]
at mx.core::Container/addChildAt()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\Container.as:2200]
at mx.core::Container/addChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\Container.as:2140]
at ImageManipulation/createAnotherImage()[E:\Projects\ImageM\src\ImageManipulation.mxml:13]
at ImageManipulation/__createBitmap_click()[E:\Projects\ImageM\src\ImageManipulation.mxml:23]


Can someone please explain why it is happening and what is the workaround ?

I am adding source below.............




<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[

private function createAnotherImage(source:Image):void{
var data:BitmapData = Bitmap(source.content).bitmapData;
var bitmap:Bitmap = new Bitmap(data);
bitmap.x = 200;
bitmap.y = 200;
trace(bitmap);
addChild(bitmap);

}
]]>
</mx:Script>
<mx:Canvas x="37" y="37" width="728" height="551" backgroundColor="white" id="canvasImage" label="Manipulating Image">
<mx:Image x="100" y="100" width="402" height="302" id="myImage" scaleContent="true" autoLoad="true"
source="assets/1.jpg" />
<mx:Button x="533" y="28" label="create Bitmap" width="111" height="29" id="createBitmap" click="createAnotherImage(myImage)"/>


</mx:Canvas>

</mx:WindowedApplication>

wvxvw
01-19-2009, 06:57 PM
1. You're not adding it to the canvas.
2. Read what the error says: cannot convert flash.display::Bitmap@1691581 to mx.core.IUIComponent.
This means that you cannot add non UIComponent deriving objects to the mx.core::Container.

Ninja_Warrior
01-19-2009, 07:23 PM
Thanks for reply ... but even replacing addChild(bitmap); with canvasImage.addChild(bitmap); gives same error ...
--------------
I understand now.. I will need a wrapper for this Bitmap object which will extend UIComponent

wvxvw
01-19-2009, 07:50 PM
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="completeHandler()">
<mx:Script>
<![CDATA[
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

public function completeHandler():void
{
var bd:BitmapData = new BitmapData(10, 10, true, 0x00FFFFFF);
bd.fillRect(new Rectangle(0, 0, 5, 5), 0xFF000000);
var nbd:BitmapData = new BitmapData(300, 300, true, 0x00FFFFFF);
nbd.merge(bd, new Rectangle(0, 0, 10, 10), new Point(0, 0), 0xFF, 0xFF, 0xFF, 0xFF);
var i:int;
while (i < nbd.width) nbd.scroll(i += 10, 0);
i = 0;
while (i < nbd.height) nbd.scroll(0, i += 10);
var bmp:Bitmap = new Bitmap(nbd);
canvas0.rawChildren.addChild(bmp);
canvas0.invalidateDisplayList();
}
]]>
</mx:Script>
<mx:Panel width="100%" height="100%"
horizontalAlign="center" verticalAlign="bottom">
<mx:Canvas id="canvas0" width="300" height="300" />
</mx:Panel>
</mx:Application>

But I'd use repeaters instead.

Ninja_Warrior
01-19-2009, 07:54 PM
Hi Thanks a lot.. its working now