Jump to content
Search Community

issue about TransFormManager.SCALE_WIDTH_AND_HEIGHT

tonilopez test
Moderator Tag

Recommended Posts

Hello,

 

I have difficulty using the TransformManager.SCALE_WIDTH_AND_HEIGHT feature.

Perhaps I have not understood the documentation or this is not the mission of this feature. What I want is to scale an object containing text that the text does not scale, eg

 

 



import com.greensock.transform.TransformManager;
import com.greensock.transform.TransformItem;
import com.greensock.events.TransformEvent;

var _manager:TransformManager = new TransformManager({hasSelectableTex:true});
var item:TransformItem = _manager.addItem(textBox,TransformManager.SCALE_WIDTH_AND_HEIGHT,true);//textBox is a movieClip that contains a text field

item.addEventListener (TransformEvent.SCALE, onUpdateItem);

function onUpdateItem (event:TransformEvent):void {
trace (textBox.width,textBox.height,textBox.scaleX,textBox.scaleY);
}

 

I appreciate your help

 

I attach a basic example of what I try to do.

[Deleted example because it contained TransformManager files, thus granting free access to anyone in the forums] :-o

Link to comment
Share on other sites

The SCALE_WIDTH_AND_HEIGHT is simply a mode that alters the target's "width" and "height" properties instead of "scaleX" and "scaleY" which can be useful for TextField instances because that'll just resize the box but not the text. However, in your example, your target is a MovieClip, not a TextField, and Flash always scales MovieClips (and their contents) when you alter their "width" and "height" properties. In order to get the behavior you want, you have two options:

 

1) Don't wrap your TextField in a MovieClip. Instead, use the TextField itself as the item that gets transformed. That way, changes to the width/height only affect the size of the box but they don't affect the scale of the text.

 

2) Create your own custom class that extends Sprite or MovieClip and override the "width" and "height" getters/setters so that they manually alter your TextField's width/height (sort of like a proxy). Use this custom class as the wrapper for your TextField instead of a MovieClip.

 

I hope that helps.

Link to comment
Share on other sites

Thanks for the reply, indeed had not understood the documentation.

 

[Deleted example because it contained TransformManager files, thus granting free access to anyone in the forums] :-o

 

Sorry about this, I had not thought of.

 

Regards

Link to comment
Share on other sites

Following your advice I have written two solutions, I posted the code, I hope it can be useful to someone.

 

Solution 1 (the very ease way):

 

import com.greensock.transform.TransformManager;
import com.greensock.transform.TransformItem;
import com.greensock.events.TransformEvent;

import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;

var _sprite:Sprite;
var _textField:TextField;
var _manager:TransformManager;
var _item:TransformItem;

_sprite = new Sprite()  ;
addChild (_sprite);

_textField = new TextField();
_textField.multiline = true;
_textField.wordWrap = true;
_textField.type = TextFieldType.INPUT;
_textField.border = true;
_textField.text = 'Texto';
_sprite.addChild (_textField);


_manager = new TransformManager();
_item = _manager.addItem(_sprite,TransformManager.SCALE_WIDTH_AND_HEIGHT,true);
_item.addEventListener (TransformEvent.SCALE, onUpdateItem);

function onUpdateItem (event:Event):void {
_textField.width = event.target.width;
_textField.height = event.target.height;
}

 

Solution 2 (The most sophisticated ) :-P

 

Main.as

 


package com{

import com.greensock.transform.TransformManager;
import com.greensock.transform.TransformItem;
import com.CustomSprite;
import com.CustomEvent;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextFieldType;
import flash.display.Sprite;

public class Main extends Sprite {

var _textField1:TextField = new TextField();
var _textField2:TextField = new TextField();
var _customSprite:CustomSprite;
var _customSprite2:CustomSprite;
var item1:TransformItem;
var item2:TransformItem;
var _manager:TransformManager;

public function Main () {

_textField1.multiline = true;
_textField1.wordWrap = true;
_textField1.type = TextFieldType.INPUT;
_textField1.border = true;
_textField1.text = 'Texto';

_textField2.multiline = true;
_textField2.wordWrap = true;
_textField2.type = TextFieldType.INPUT;
_textField2.border = true;
_textField2.text = 'Texto 2';

_customSprite = new CustomSprite  ;
addChild (_customSprite);
_customSprite.target = _textField1;

_customSprite2 = new CustomSprite  ;
addChild (_customSprite2);
_customSprite2.target = _textField2;

_manager = new TransformManager();

item1 = _manager.addItem(_customSprite,TransformManager.SCALE_WIDTH_AND_HEIGHT,true);
item2 = _manager.addItem(_customSprite2,TransformManager.SCALE_WIDTH_AND_HEIGHT,true);

_customSprite.addChild (_textField1);
_customSprite.addEventListener (CustomEvent.WIDTH_CHANGED, widthChangedListener, false, 0, true);
_customSprite.addEventListener (CustomEvent.HEIGHT_CHANGED, heightChangedListener, false, 0, true);

_customSprite2.addChild (_textField2);
_customSprite2.addEventListener (CustomEvent.WIDTH_CHANGED, widthChangedListener, false, 0, true);
_customSprite2.addEventListener (CustomEvent.HEIGHT_CHANGED, heightChangedListener, false, 0, true);
_customSprite2.x = 250;
}


private function widthChangedListener (event:CustomEvent):void {
event.target.target.width = event.target.width;
}

private function heightChangedListener (event:CustomEvent):void {
event.target.target.height = event.target.height;
}
}
}

 

CustomSprite.as

 

package com{
import flash.display.Sprite;
import flash.events.Event;

import com.CustomEvent;
import flash.text.TextField;

public class CustomSprite extends Sprite {

private var _target:TextField;

public function CustomSprite () {
super ();
}

override public function set  width (value:Number):void {
super.width = value;
dispatchEvent (new CustomEvent(CustomEvent.WIDTH_CHANGED));
}

override public function get width ():Number {
return super.width;
}

override public function set  height (value:Number):void {
super.height = value;
dispatchEvent (new CustomEvent(CustomEvent.HEIGHT_CHANGED));
}

override public function get height ():Number {
return super.height;
}


override public function dispatchEvent (event:Event):Boolean {
if (willTrigger(event.type)) {
return super.dispatchEvent(event);
}
return true;
}

public function set target(target:TextField):void{
_target = target
}

public function get target():TextField{
return _target;
}
}
}

 

CustomEvent.as

 

package com {
import flash.events.Event;

public class CustomEvent extends Event {

public static const WIDTH_CHANGED:String = "widthChanged";
 public static const HEIGHT_CHANGED:String = "heightChanged";

public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) {
	super(type, bubbles, cancelable);
}

override public function clone():Event {
	return new CustomEvent(type, bubbles, cancelable);
}

override public function toString():String {
	return formatToString("CustomEvent", type, bubbles, cancelable, eventPhase);
}
}}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...