سؤال

I linked an item in library with an .as file.In this .as file,I write the class like this:

public class TextArea extends MovieClip 
{
    .......
    public function setWidth(w:Number):void
    {
         this.width = w;
    }
}

Then I change the item to complied clip in library(It is same as complied as ***SWC* )**.I add it into stage and change it's property to fire the setWidth function.But the width just doesn't change.What should I do to change the Movie Clip's width?

P.S. The child ,a TextField , in this item can be changed width.

enter image description here

In the pic, I put a SWC component on stage and the SWC only have one TextField as child.

The Text field is given black border and the SWC is being selected,the blue border is its bounds.

And I print the this.width in text and the width is auto changed to adjust the text field.

But the blue border is what I want to change.It shows on Flash panel and stand for the transform bounds like this:

enter image description here

So, I don't have any trouble to change children's data. And now I found the class is like this:

enter image description here

There is a LivePreviewParent as this.parent. It provide the live preview of my swc on stage.And the size I change is absolutely changed,but I think the blue border is stand for the LivePreviewParent's border.

However, change the size of LivePreviewParent is equal to change the component's size not the blue one.How strange the class is ! So I want to know the blue one is actually the LivePreviewParent's border? If yes , how to change it?

هل كانت مفيدة؟

المحلول

I don't know, how is exactly your component builded, you should have inner logic to resize subcomponents/childs.

Universal solution, blind resize of every child:

public function setWidth(w:Number):void
{
    var i: uint, len: uint = this.numChildren, child: DisplayObject;

    for(i; i < len; ++i){
        this.getChildAt(i).width = w;
    }

}

If only textArea is one child:

public function setWidth(w:Number):void
{
    textArea.width = w;

}

Simple TextField container, with background and custom padding:

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class TextContainer extends Sprite {
    private var _textField:TextField;
    private var _padding:int;

    public function TextContainer(text:String, format:TextFormat, padding:int = 4) {
        _padding = padding;
        _textField = new TextField();
        _textField.defaultTextFormat = format;
        _textField.autoSize = TextFieldAutoSize.LEFT;
        _textField.multiline = true;
        _textField.wordWrap = true;
        _textField.text = text;

        addChild(_textField);

        _textField.x = _textField.y = padding;

        //Default width
        setWidth(100);
    }

    public function setWidth(value:Number):void {
        _textField.width = value;

        graphics.clear();
        graphics.beginFill(0xF0F0F0);
        graphics.drawRect(0, 0, _textField.width + 2*_padding, _textField.height + 2*_padding);
    }
}

Usage:

var textContainer: TextContainer = new TextContainer("Some text here for test, more text, for multi-line test and resize", new TextFormat("Arial"));
desiredContainer.addChild(textContainer);
textContainer.setWidth(200);

نصائح أخرى

You have to change the width of 'this' relative to something. The most likely thing is its container. So let's assume that your TextArea class is instantiated in your Main class as 'textarea' In Main you'd write this: textarea.width = 500

Finally I found that there is an impassable wall between Flash IDE and the AS code in SWC. There is no way to access IDE's data from SWC.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top