Search code examples
actionscript-3flashtextspritetween

Flash AS3 - Changing sprite size effects textifeld?


Struggling with a simple one. I have a sprite, and a textfield. Neither are added as children of each other, they are separate objects. The class is shown below in full. All works as expected, however when I call the simpleGUIElementResize function to resize the sprite the textfield also resizes, which I don't want. The effect should be that I can do whatever I want and the text stays where it is.

Any ideas?

Thank you.

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});
    }

}

}


Solution

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