Question

I'm trying to detect overlap between elements on my canvas. The attempt is to reuse some of the code for collision detection at http://www.gskinner.com/blog/archives/2005/08/flash_8_shape_b.html

This is the smallest MXML sample I could come up with that gives me a type error.

    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
      creationComplete="init()">
 <fx:Script>
  <![CDATA[
   public function init():void {
    var matrix:Matrix = new Matrix();
    var bounds:Rectangle = this.getBounds(this);
    var img:BitmapData = new BitmapData(this.width, this.height);
    img.draw(this,matrix,new ColorTransform(1,1,1,1,255,-255,-255,255));
    var bm:Bitmap = new Bitmap(img);
    bm.x = 0;
    bm.y = 0;
    canvas.addChild(bm);
   }
  ]]>
 </fx:Script>
 <mx:Canvas id="canvas" width="600" height="600">
  <s:Label id="text" x="100" y="100">
   This is a test
  </s:Label>
 </mx:Canvas>
</s:Application>

Running this code fails with

Main Thread (Suspended: TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@3b81971 to mx.core.IUIComponent.) 

mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild mx.core::Container/addChildAt mx.core::Container/addChild scratch/init scratch/___scratch_Application1_creationComplete flash.events::EventDispatcher/dispatchEventFunction [no source] flash.events::EventDispatcher/dispatchEvent [no source] mx.core::UIComponent/dispatchEvent mx.core::UIComponent/set initialized mx.managers::LayoutManager/doPhasedInstantiation mx.managers::LayoutManager/doPhasedInstantiationCallback

Can anyone see what I am missing? Thanks in advance for all help. -v

Was it helpful?

Solution

You're trying to add a BitMap as a child on a Canvas. The addChild method accepts a DisplayObject, which the BitMapData is not.

You can probably set the Bitmap to the source of an Image and add that as a child. Although I'm not specifically sure why you aren't getting an error about conversion for Bitmap to DisplayOBject as opposed to UIComponent.

On an unrelated note, the fact that you have "this is a test" as plain text in the label looks wrong to me; does Flex 4 know what to do with that?

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