开发者

flex: assigning id to primitives

开发者 https://www.devze.com 2023-02-28 03:14 出处:网络
I have created a few flex p开发者_如何转开发rimitives on screen via action script. Now based on business logic, I need to make changes to some of these primitives such as: changing thickness, directio

I have created a few flex p开发者_如何转开发rimitives on screen via action script. Now based on business logic, I need to make changes to some of these primitives such as: changing thickness, direction of arrowhead etc

One thought was to assign id's to these primitives as they are built- how do I do that?

e.g. I get a line built as below

var myShape:Shape=new Shape();    
myShape.graphics.lineStyle(thickness,color);    
myShape.graphics.moveTo(XFrom,YFrom);    
myShape.graphics.lineTo(XTo,YTo);

If, based on some condition, I want to change the color/ thickness of above, how do i reference this line above?


It'd imagine this is done within a component of some sort. All UIComponents have a lifecycle which you want to adhere to. You can override several functions to get the kind of functionality you want. Like this:

private var _shape:Shape;

override protected function createChildren():void
{
   super.createChildren();
   if(!this._shape)
   {
      this._shape = new Shape();
      addChild(this._shape);
   }

}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
   super.updateDisplayList(unscaledWidth, unscaledHeight);

   this._shape.graphics.lineStyle(thickness,color);    
   this._shape.graphics.moveTo(XFrom,YFrom);    
   this._shape.graphics.lineTo(XTo,YTo);
}

And if you want it to run when you data changes, you should look into invalidateDisplayList which you would call after whatever data has changed and it would run the updateDisplayList function on the next frame.


I came up with a more easier approach

Basically instead of drawing the primitives directly, assigned the draw to a shape function which will return a Shape object.

Now the primitive line has a easy handler to change properties. let me know if you need more specific details on how to achieve this

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号