在调整大小时垂直居中动画片段

时间:2013-02-05 15:58:11

标签: flash actionscript actionscript-2

我的公司有一个可以在线阻止各种内容类型的产品,我正在设计阻止*.swf文件的时候。我在Flash工作已经很多年了,我需要一些帮助。 必须使用ActionScript 2编写,以确保最大程度的兼容性!

我有一个4帧的影片剪辑,我根据舞台大小/比例切换帧以显示不同的徽标布局。但在所有情况下,徽标应该以我遇到问题的舞台为中心。它非常接近于居中,但在某些情况下,徽标会被切断。

有问题的代码在第一帧。一般来说,我所做的只是将影片剪辑置于中心位置:

Stage.align = "TC";
Stage.scaleMode = "noscale";

var blockedContent:MovieClip = _root.mc_blocked;
var stageListener:Object = new Object();
stageListener.onResize = function() {
    blockedContent._y = (Stage.height/2)-(blockedContent._height/2);`
};
Stage.addListener(stageListener);

但这里是完整的代码,所以你可以在上下文中看到它。

// set the Flash movie to have a fixed anchor in the top top center of the screen.
Stage.align = "TC";
// prevent the Flash movie from resizing when the window changes size.
Stage.scaleMode = "noscale";

//--------------------------------------------------------------------
//Add a new listener when the stage is resized
//--------------------------------------------------------------------
var stageListener:Object = new Object();
stageListener.onResize = function() {
    fixLayout();
};
Stage.addListener(stageListener);

//--------------------------------------------------------------------
//Define some variables
//--------------------------------------------------------------------
var blockedContent:MovieClip = _root.mc_blocked;
var StageRatio:Number = 0;
var Sizes:Object = {
    Padding:20,
    VLarge:{w:0, h:0},
    VSmall:{w:0, h:0},
    HLarge:{w:0, h:0},
    HSmall:{w:0, h:0}
};
//--------------------------------------------------------------------
//duplicate the clip, and save the sizes for each frame
//--------------------------------------------------------------------
duplicateMovieClip(blockedContent, "mc_duplicate", _root.getNextHighestDepth());
mc_duplicate._alpha = 0;
mc_duplicate.gotoAndStop(1);
Sizes.VLarge.w = mc_duplicate._width;
Sizes.VLarge.h = mc_duplicate._height;
mc_duplicate.gotoAndStop(2);
Sizes.HLarge.w = mc_duplicate._width;
Sizes.HLarge.h = mc_duplicate._height;
mc_duplicate.gotoAndStop(3);
Sizes.VSmall.w = mc_duplicate._width;
Sizes.VSmall.h = mc_duplicate._height;
mc_duplicate.gotoAndStop(4);
Sizes.HSmall.w = mc_duplicate._width;
Sizes.HSmall.h = mc_duplicate._height;
//Remove it, we are done with it
mc_duplicate.removeMovieClip();

//Uncomment to see if it is centered
//mc_duplicate._alpha = 25;
//mc_duplicate.gotoAndStop(1);

//--------------------------------------------------------------------
//Fix the layout and scale things appropriately
//--------------------------------------------------------------------
function fixLayout() {

    StageRatio = Stage.width/Stage.height;
    //derermine which way to scale the logo
    if (Stage.height-Sizes.Padding>Sizes.VLarge.h+Sizes.Padding || (StageRatio>0.67 && StageRatio<2.45)) {
        if (Stage.width-Sizes.Padding<Sizes.VSmall.w+Sizes.Padding) {
            //Small vertical layout (no logo)
            blockedContent.gotoAndStop(3);
        } else {
            //default state - the large vertical layout
            blockedContent.gotoAndStop(1);
        }
    } else {
        //if it's not as tall as the large vertical...
        if (Stage.width-Sizes.Padding<Sizes.HSmall.w+Sizes.Padding) {
            //Use the small horizontal (no logo)
            blockedContent.gotoAndStop(4);
        } else {
            //Use the large horizontal
            blockedContent.gotoAndStop(2);
        }
    }
    //make sure it's never larger than 100%
    blockedContent._xscale = blockedContent._yscale=100;

    //Resize by the height...
    if (blockedContent._height+Sizes.Padding>Stage.height-Sizes.Padding) {
        blockedContent._height = Stage.height-Sizes.Padding;
        //Match the scales
        blockedContent._xscale = blockedContent._yscale;
    }
    //Resize by the width...  
    if (blockedContent._width+Sizes.Padding>Stage.width-Sizes.Padding) {
        blockedContent._width = Stage.width-Sizes.Padding;
        //Match the scales
        blockedContent._yscale = blockedContent._xscale;
    }

    //Center it up   
    blockedContent._y = (Stage.height/2)-(blockedContent._height/2); //-Sizes.Padding*2;
}

//--------------------------------------------------------------------
//Fade it in, and initially call the layout fix
//--------------------------------------------------------------------
blockedContent._alpha = 0;
setTimeout(function(){
    blockedContent.onEnterFrame=function(){
        this._alpha+=3;
        if(this._alpha>=100){
            delete this.onEnterFrame;
        }
    }
},300);
fixLayout();

1 个答案:

答案 0 :(得分:0)

blockedContent MovieClip内的内容已经居中。因此,您所要做的就是根据舞台高度(除以一半)使MC居中。只需删除代码的(blockedContent._height/2)部分:

blockedContent._y = (Stage.height/2);