haXe Forum > How to center something in Flash
-
Nick Sabalausky Aug 24 at 04:24
Consider this simple Haxe/Flash app that covers the screen with a big rectangle:
.
test.hxml:-swf test.swf -main Test -swf-version 7 -swf-header 400:300:12:FFFFFF --flash-strict
test.hx:
import flash.MovieClip; class Test extends MovieClip { static function main() { var mc:flash.MovieClip = flash.Lib._root; mc.beginFill(0xFF0000); mc.moveTo(0, 0); mc.lineTo(400, 0); mc.lineTo(400, 300); mc.lineTo(0, 300); mc.endFill(); } }
Run that the swf, and then resize it. Notice that when the window has a different aspect ratio than the swf, the rectangle is aligned to the top-left.
.
Now consider this ActionScript 2 code (put it in the first fame of a blank 400x300 fla):
.stop(); var mc = _root; mc.beginFill(0xFF0000); mc.moveTo(0, 0); mc.lineTo(400, 0); mc.lineTo(400, 300); mc.lineTo(0, 300); mc.endFill();
It's basically the same code. Compile it to an swf and run it. *This* time, when you resize, the rectangle stays in the center.
.
I need this centering behavior. How can I do it, or emulate it, in Haxe/Flash? Preferably without JS. If I have to hack the haxe std lib, that's fine. Or if I have to hack the haxe compiler, that's fine too as long as someone who knows OCaml can provide specific instructions on what to change to what (I'm completely lost with OCaml code). -
you use an onresize event ie: http://www.kirupa.com/forum/showthread.php?t=265495
-
Nick Sabalausky Aug 25 at 03:05
Thanks, that set me off on the right track. Turns out this is what I needed:
import flash.MovieClip; class Test extends MovieClip { static function main() { var mc:flash.MovieClip = flash.Lib._root; mc.beginFill(0xFF0000); mc.moveTo(0, 0); mc.lineTo(400, 0); mc.lineTo(400, 300); mc.lineTo(0, 300); mc.endFill(); var resizeListener:Dynamic = {}; resizeListener.onResize = function():Void { trace("In onResize"); // Adjust main MC's xscale and yscale here to // mimic the default scale mode, and then // adjust main MC's x and y to center it. // Get current swf dimensions with // flash.Stage.width and flash.Stage.height }; flash.Stage.addListener(resizeListener); flash.Stage.scaleMode = "noScale"; } }
-
Nick Sabalausky Sep 16 at 10:33
I've stumbled on a much easier (albiet less flexible) way, if anyone's interested:
flash.Stage.align = ""; /* Possible values for flash.Stage.align are: "T": Top aligned, Horizontally centered "B": Bottom aligned, Horizontally centered "L": Left aligned, Vertically centered "R": Right aligned, Vertically centered "TL": Top-Left aligned "TR": Top-Right aligned "BL": Bottom-Left aligned "BR": Bottom-Right aligned Anything else: Vertically and Horizontally centered */