设置动画片段图像/背景

时间:2018-01-23 09:15:41

标签: actionscript-3 actionscript movieclip

我有一个按钮和一个小区域,这是一个动画片段。

我需要的是,按下按钮会将图像插入到movieClip中,它必须基本上停靠在整个动画片段区域。

我查看了多篇帖子但他们有大量的信息我无法弄清楚,这是迄今为止我所做的:

B_Background1.addEventListener(MouseEvent.CLICK, setBackground1);

function setBackground1(event:MouseEvent):void{
    var firstPic:MovieClip = new C_1BackgroundPIC();

    addChildAt(firstPic, C_MainStage);
}

由于了解它,它会向按钮添加一个事件,然后在该函数中创建一个新的movieClip实例,其中包含Picture,然后将其添加到&#34 ; MainStage的"虽然使用C_MainStage是无效的,因此不起作用,如果我只使用0作为位置,它会添加一张图片但是然后将它添加到位置0,我不想要...

1 个答案:

答案 0 :(得分:0)

这是一个好的开始。当您使用addChildAt时,它需要一个数字作为第二个参数。据推测,C_MainStage是您的MovieClip(不是数字)。大概你想要当前显示索引C_MainStage(不是C_MainStage本身)。这可以通过使用getChildIndex

来实现

请看以下内容:

function setBackground1(event:MouseEvent):void{
    var firstPic:MovieClip = new C_1BackgroundPIC();

    addChildAt(firstPic, getChildIndex(C_MainStage));

    //now, you may need to also resize your picture so it fits in the bounds of C_MainStage, if so, you can do something like this:
    if(firstPic.width >= firstPic.height){
        //if firstPic is wider than it is tall

        //set it's width to the same as C_MainStage
        firstPic.width = C_MainStage.width;

        //now, to keep the aspect ratio (in case they don't match), make the scaleY the same value as the new scaleX
        firstPic.scaleY = firstPic.scaleX
    }else{
        //do the opposite as above since the height is larger than the width
        firstPic.height = C_MainStage.height;
        firstPic.scaleX = firstPic.scaleY;
    }

    //now, you may want to center firstPic in the C_MainStage bounds
    firstPic.x = C_MainStage.x + ((C_MainStage.width - firstPic.width) * .5);
    firstPic.y = C_MainStage.y + ((C_MainStage.height - firstPic.height) * .5)
}