0

如果我有一个容器,里面有儿童展示对象。容器的大小由孩子的大小决定。当大小改变时,我需要做一些事情来适应新的大小。

因为我没有找到检测它的那种事件,所以我使用 ENTER_FRAME 事件,这很愚蠢。保持最后一帧的大小,并与当前帧进行比较。可能在处理输入帧事件后大小发生了变化,因此在某些情况下,您可能会在下一帧中看到正确的结果。

我认为这不是一个好的组件解决方案。给我你的想法,谢谢。

4

2 回答 2

2

好吧,没有标准的解决方案。您可以创建一个自定义类,它会覆盖 width/height/x/y/scaleX/scaleY/scrollRect .. 以及其他一些属性的设置器。孩子们应该扩展这样的课程。

我使用了一个布尔值来防止它被多次分派,一帧后标志将被重置。

override public function set width(value:Number):void
{
    if (value !== super.width && !isNaN(Number(value)) this.dispatchResize();
    super.width = value;
}

override public function set height(value:Number):void
{ 
    if (value !== super.height && !isNaN(Number(value)) this.dispatchResize();
    super.height = value;
}

override public function set scaleX(value:Number):void
{ 
    if (value !== super.scaleX && !isNaN(Number(value)) this.dispatchResize();
    super.scaleX = value;
}

override public function set scaleY(value:Number):void
{ 
    if (value !== super.scaleY && !isNaN(Number(value)) this.dispatchResize();
    super.scaleY = value;
}

private var _hasDispatchedResize:Boolean;
protected function dispatchResize():void
{
   // do something
   if (!this._hasDispatchedResize)
   {
      this.dispatchEvent(new Event(Event.RESIZE));
      this._hasDispatchedResize = true;
      this.addEventListener(Event.ENTER_FRAME, handleEnterFrameOnce);
   }
}

private function handleEnterFrameOnce(event:Event):void
{
    this.removeEventListener(Event.ENTER_FRAME, handleEnterFrameOnce);
    this._hasDispatchedResize = false;
}

现在,在容器类中你可以听一个Event.RESIZE孩子。您不确定该值是否实际发生了变化(如果电影剪辑上的帧发生变化),但在大多数情况下这会起作用。在调度调整大小之前,我在这个设置器中添加了一个额外的检查。这是否适合您的情况取决于具体情况。

于 2012-10-16T06:27:54.960 回答
0

我要做的是调整孩子大小的任何函数,将您自己的调度事件添加到它将广播发生大小更改的事件。

//Never forget to import your classes.
import flash.events.EventDispatcher;
import flash.events.Event;

//Our custom listener listens for our custom event, then calls the function we
//want called when children are resized.
addEventListener("childResized", honeyIResizedTheChildren);

function resizingLikeFun(){
    datClip.width++;//they be resizin, yo.
    dispatchEvent(new Event("childResized"));//Our custom event
}

function honeyIResizedTheChildren(e:Event){
    trace("Giant Cheerios");
    //Whatever you want to do when the children are resized goes here.
}

我假设该代码有一些事情,所以如果这并不完全适用于您,请告诉我。

于 2012-10-16T04:46:56.137 回答