0

与一个简单的斗争。我有一个精灵和一个文本字段。两者都不是作为彼此的子对象添加的,它们是单独的对象。该类如下所示。一切都按预期工作,但是当我调用 simpleGUIElementResize 函数来调整精灵的大小时,文本字段也会调整大小,这是我不想要的。效果应该是我可以做任何我想做的事情,并且文本保持在原来的位置。

有任何想法吗?

谢谢你。

package com.simpleGUI {
    //flash imports
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.text.Font;

//greensock
import com.greensock.TweenMax;
import com.greensock.easing.*;


public class simpleGUIElement extends Sprite {
    //declaration vars
    private var sgeWidth:int;
    private var sgeHeight:int;
    private var sgeColour:String;
    private var sgeText:String;

    //internal vars
    private var sgeSprite:Sprite = new Sprite();
    private var sgeTextField:TextField = new TextField();
    private var sgeFormat:TextFormat = new TextFormat();

    //formatting vars
    private var sgeTextSize:int;

    public function simpleGUIElement(SGEWIDTH:int, SGEHEIGHT:int, SGECOLOUR:String, SGETEXT:String, SGETEXTSIZE:int) {
        trace('simpleGUIElement created');

        //populate the vars
        sgeWidth = SGEWIDTH;
        sgeHeight = SGEHEIGHT;
        sgeColour = SGECOLOUR;
        sgeText = SGETEXT;

        sgeTextSize = SGETEXTSIZE;

        createSprite();  //create the rectangle

        //if the text string is not null create the textfield
        if (sgeText.indexOf("null") >= 0) {
            return;
        } else {
            createTextField();
        }

    }

    private function createSprite():void {
        //create the sprite
        sgeSprite.graphics.beginFill(uint(sgeColour));
        sgeSprite.graphics.drawRect(0, 0, sgeWidth, sgeHeight);
        sgeSprite.graphics.endFill();
        //add the sprite
        addChild(sgeSprite);
    }

    private function createTextField():void {
        //sort of the formatting
        sgeFormat.size = sgeTextSize;
        sgeFormat.align = TextFormatAlign.LEFT;
        sgeFormat.font = 'Arial';
        sgeFormat.color = (0xffffff);
        sgeFormat.leading = 5;
        sgeFormat.kerning = 10;
        sgeFormat.letterSpacing = 0.7;

        //create the textfield
        sgeTextField.defaultTextFormat = sgeFormat;
        sgeTextField.htmlText = sgeText;
        sgeTextField.width = sgeWidth;
        sgeTextField.height = sgeHeight - 20;
        sgeTextField.wordWrap = true;
        sgeTextField.selectable = false;
        sgeTextField.x = 10;
        sgeTextField.y = 10;
        sgeTextField.mouseEnabled = false;
        addChild(sgeTextField);
    }


    public function simpleGUIElementResize(animTime:int, newWidth:int, newHeight:int):void {
        TweenMax.to(sgeSprite, animTime, {width:newWidth, height:newHeight});
    }

}

}

4

1 回答 1

2

Sorry but you code works.
Live demo here (click on stage to resize).

于 2012-08-02T12:09:23.597 回答