Reputation: 47
Is it possible to set a textField on stage dynamically and allow for stageHeight & width changes with resize event.
Stage say (500 x 250), doesn't really matter as example and any text, (xml, inline, whatever). I took this example from republicofCode as an example as I've spent all day trying different theories and have convinced myself that it's not humanely possible, despite in appearance being the simplest thing ever. Please HELP resolve the mystery at least. . .
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var myText:TextField = new TextField();
myText.text = "The quick brown fox jumps over the lazy dog";
myText.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
addChild(myText);
myText.wordWrap = true;
myText.width = 150;
myText.height = 40;
myText.x = 100;
myText.y = 100;
function onAddToStage(event:Event):void {
stage.addEventListener(Event.RESIZE, adjustTextField);
trace("text definitely onStage");
}
function adjustTextField(event:Event):void {
trace ("adjustTextField being fired!");
myText.x = stage.stageWidth / 2; // ANYTHING THAT WORKS
// myText.y = stage.stageWidth / 2; //ANYTHING THAT WORKS
trace ("adjustTextField fired and still nothing happening. . . !");
trace ("height" +stage.stageHeight);
trace ("width" +stage.stageWidth);
}
What gives?
If you copy & paste the code you should get a textField that positions itself at x.100 & y.100, in theory, by scaling the flash player it should then move the top left corner of the textField to the center of the scene. But NO. ?! WTD!
I'm not after rescaling the text, just altering it's position and the width of the textBox to accomodate a larger or smaller text area subject to the stage either being enlarged.
I've googled for a day and not seen a single example of this working so maybe it just can't be done but surely something so trivial is possible, after everything else you can do in flash, surely this is beyond complication!
Upvotes: 0
Views: 626
Reputation: 10665
You should read about scaleMode.
I added
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
to your example code to make it work.
(I also had to move the ADDED_TO_STAGE listener up, to add it before the addChild call, but if you are already seeing your traces you don't need to change that)
Upvotes: 0