Sunday, March 28, 2010

Drawing Class works in Flash but not in...

I have this simple class that I've been using in Flash, and it works fine, and i wanted to use it in Flex, but for whatever reason it won't work.?I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert net.creativedynamix.drawing::DrawBox@1d8e341 to mx.core.IUIComponent.

I tried just adding it:

var drawArea:DrawBox = new DrawBox();

addChild(drawArea);

and I got that error... so I just figured I'd try to add it to a canvas or something, so made a canvas and named it ''cnvs'' and did:

var drawArea:DrawBox = new DrawBox();

cnvs.addChild(drawArea);

why can't you do this like you can in flash?

I thought AS3 was supposed to work in both?

well I tried to upload the AS file, but it won't let me for some reason, but its not big, so here it is:

package net.creativedynamix.drawing
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class DrawBox extends Sprite
{
?private var _startX:Number;
?private var _startY:Number;
?private var _endX:Number;
?private var _endY:Number;
?private var _drawWidth:Number;
?private var _drawHeight:Number;
?
?private var _drawColor:Number = 0xFFFF00;
?
?private var baseSprite:Sprite;
?private var drawingSprite:Sprite;
?
?public function DrawBox():void {
addEventListener(Event.ADDED_TO_STAGE, imAdded);
?}
?
?private function imAdded(event:Event):void {
baseSprite = new Sprite();
baseSprite.x = 0;
baseSprite.y = 0;
baseSprite.graphics.beginFill(0x000000, .01);
baseSprite.graphics.drawRect(0, 0, 2000, 2000);
baseSprite.graphics.lineStyle(0, 0x000000, 0);
baseSprite.graphics.endFill();

baseSprite.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);

drawingSprite = new Sprite();

addChild(baseSprite);
?}
?
?
?private function startDrawing(event:MouseEvent):void
?{
_startX = this.mouseX;
_startY = this.mouseY;

drawingSprite.graphics.clear();

baseSprite.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
baseSprite.addEventListener(MouseEvent.MOUSE_MOVE, doDrawing);
baseSprite.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

drawingSprite.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

?}
?
?private function doDrawing(event:MouseEvent):void {
drawingSprite.graphics.clear();

_drawWidth = this.mouseX - _startX;
_drawHeight = this.mouseY - _startY;

drawingSprite.graphics.beginFill(_drawColor, .2);
drawingSprite.graphics.lineStyle(1, _drawColor, .5);
drawingSprite.graphics.drawRect(_startX, _startY, _drawWidth, _drawHeight);
drawingSprite.graphics.endFill();

baseSprite.addChild(drawingSprite);

?}
?
?private function stopDrawing(event:MouseEvent):void {
baseSprite.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
baseSprite.removeEventListener(MouseEvent.MOUSE_MOVE, doDrawing);
baseSprite.removeEventListener(MouseEvent.MOUSE_UP, stopDrawing);

dispatchEvent(new Event(DrawingEvents.DONE_DRAWING));
?}
?
?
?// Public Properties \\
?public function get startX():Number { return _startX; }
?public function get startY():Number { return _startY; }
?public function get endX():Number { return _endX; }
?public function get endY():Number { return _endY; }
?public function get drawWidth():Number { return _drawWidth; }
?public function get drawHeight():Number { return _drawHeight; }
?
?public function get drawColor():Number { return _drawColor; }
?public function set drawColor(_val:Number) { _drawColor = _val; }
}

}

Drawing Class works in Flash but not in...

You need to wrap it in a UIComponent:

import mx.core.UIComponent;

var drawArea:DrawBox = new DrawBox();

var uic:UIComponent = new UIComponent;

uic.addChild(drawArea);

this.addChild(uic);

If this post answers your question or helps, please mark it as such.

No comments:

Post a Comment