Loading...
转载链接
The release of Flex Builder 2 is around the corner and though the next version of Flash is still a ways away, ActionScript 3 will be a big part of Flex 2 and the impending release of Flash Player 9 (which arrives with Flex). ActionScript 3 is the next step forward and to help with the transition (for those of you deciding to make it), I thought, since I’ve been working with AS3 a bit lately, I’d make a new Tip of the Day thread for ActionScript 3.0 to help people prepare. So here we go: Read more…
说干就干, 先来看看 Flash 内容大小自适应到窗口.
大家可能有wallop吧, 据我观察, wallop 中就是一个宽高均为 100% 的 Flash, 当改变窗口大小时利用 Stage.onResize 事件来改变 Flash 内容的大小及位置.
看了看 Flash 帮助, 虽然 Stage 对象中有 onResize 事件, 但不能直接用 Stage.onResize, 需要加一个侦听对象, 不爽
不过 onResize 确实是个好东西, 作用不小
I like it~~~
看看我的代码吧:
PLAIN TEXT >>
ACTIONSCRIPT:
-
// 先建立一个名为 p_mask 的矩形 MC, 放置在场景中备用
-
// 之后在场景第一帧写下这里的代码
-
// 考虑到计算可能会经常使用这些数据
-
// 所以先定义几个变量备用
-
// 原始宽度
-
var ow:Number = 550;
-
// 原始高度
-
var oh:Number = 400;
-
// 改变后的宽度
-
var nw:Number;
-
// 改变后的高度
-
var nh:Number;
-
// 创建侦听对象
-
var myListener:Object = new Object();
-
// onResize 事件需要设置 Stage.scaleMode 为 noScale
-
Stage.scaleMode = "noScale";
-
// 绑定侦听器
-
Stage.addListener(myListener);
-
// 定义侦听函数
-
myListener.onResize = function() {
-
nw = Stage.width;
-
nh = Stage.height;
-
setXY();
-
};
-
// 第一次运行
-
myListener.onResize();
-
// 定义改变尺寸函数
-
function setXY() {
-
with (_root.p_mask) {
-
_x = (ow-nw)/2;
-
_y = (oh-nh)/2;
-
_width = nw;
-
_height = nh;
-
}
-
}
-
// 一切 OK, 发布预览
-
// swf 和宽高都为 100% 的 HTML 均测试通过