【问题标题】:Setting an movieclips image/background设置影片剪辑图像/背景
【发布时间】:2018-07-02 01:15:57
【问题描述】:

我有一个按钮和一个小区域,它是一个电影剪辑。

我需要的是,在按下按钮时它会将图像插入到影片剪辑中,它本质上必须停靠在整个影片剪辑区域中。

我浏览了多个帖子,但他们有大量我无法弄清楚的信息,这是我到目前为止所做的:

B_Background1.addEventListener(MouseEvent.CLICK, setBackground1);

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

    addChildAt(firstPic, C_MainStage);
}

根据的理解,它向按钮添加一个事件,然后在函数中创建一个新的movieClip实例,其中包含图片,然后将其添加到“MainStage”虽然使用C_MainStage 是无效的因此不起作用,但如果我只使用 0 作为位置,它确实会添加一张图片,但它会将其添加到我不想要的位置 0...

【问题讨论】:

  • 什么是C_MainStage
  • @Organis 主场景上的电影剪辑,基本上设置了图片应该适合的范围

标签: actionscript-3 actionscript movieclip


【解决方案1】:

这是一个好的开始。当您使用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)
}

【讨论】:

  • 我似乎无法让它工作。我注释掉了调整大小的部分,然后它添加了一张图片,但只在常规位置。我也想不出解决办法
  • 您需要更加具体。你有错误吗?什么正在发生或没有发生与您的预期?
  • @Flame - 你明白了吗?
猜你喜欢
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 2017-01-14
相关资源
最近更新 更多