如何使用此代码合并全局To Local?

时间:2011-05-17 23:50:21

标签: flash actionscript-3

我有这组代码来创建一个简单的前后图像显示器:

import com.greensock.*;
import com.greensock.easing.*;

function init() : void  {
    sliderbar_mc.buttonMode = true;
    sliderbar_mc.addEventListener(MouseEvent.MOUSE_DOWN,moveSliderbar);
    stage.addEventListener(MouseEvent.MOUSE_UP,stopSliderbar);
    mask_mc.alpha = 0;
    after_mc.mask = mask_mc;
    TweenLite.to(sliderbar_mc,4,{x:stage.stageWidth/2,ease:Elastic.easeOut});
    TweenLite.to(mask_mc,4,{x:stage.stageWidth/2,ease:Elastic.easeOut});
}  

function moveSliderbar(event:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}  

function stopSliderbar(event:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}

function mouseMoveHandler(event:MouseEvent):void{
    sliderbar_mc.x = mouseX;
    if (sliderbar_mc.x > stage.stageWidth){
        sliderbar_mc.x = stage.stageWidth;
    }
    else if(sliderbar_mc.x < 0){
        sliderbar_mc.x = 0;
    }
    mask_mc.x = sliderbar_mc.x;
}

init();

但是现在我需要将放映区域放在舞台上某个地方的动画片段中,对于我的生活我无法弄清楚如何使用globalToLocal来完成这项工作......这是我的尝试:

import com.greensock.*;
import com.greensock.easing.*;

function init():void  {
    area_mc.sliderbar_mc.buttonMode = true;
    area_mc.sliderbar_mc.addEventListener(MouseEvent.MOUSE_DOWN,moveSliderbar);
    stage.addEventListener(MouseEvent.MOUSE_UP,stopSliderbar);
    area_mc.mask_mc.alpha = 0;
    area_mc.after_mc.mask = area_mc.mask_mc;
    TweenLite.to(area_mc.sliderbar_mc,3,{x:stage.stageWidth/2,ease:Elastic.easeOut});
    TweenLite.to(area_mc.mask_mc,3,{x:stage.stageWidth/2,ease:Elastic.easeOut});
}  

function moveSliderbar(event:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}  

function stopSliderbar(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}

function mouseMoveHandler(event:MouseEvent):void {
    var topLeft:Point = area_mc.localToGlobal(new Point(0, 0));
    var bottomRight:Point = area_mc.localToGlobal(new Point(width, height));
    area_mc.sliderbar_mc.x = area_mc.mouseX;
    if (area_mc.mouseX > topLeft.x) {
        area_mc.sliderbar_mc.x = topLeft.x;
    }
    else if(area_mc.mouseX < bottomRight.x){
        area_mc.sliderbar_mc.x = bottomRight.x;
    }
    area_mc.mask_mc.x = area_mc.sliderbar_mc.x;
}

init();

显然它不能正常工作,我知道这一切都在mouseMoveHandler函数中,有人可以给我一些指示吗?

1 个答案:

答案 0 :(得分:1)

好的,我玩了一下这个,我想我能够做到你想要的。它仍然有很多问题需要解决,而且代码很丑陋,但我试图继续使用你想要使用的方法。为此,我在舞台上创建了所有必要的剪辑。我假设area_mc是您尝试在其中工作的剪辑,其他所有内容都在其中。

明天我可能会再多做一点,但在这里;告诉我它是否正在做你需要的事情:

var maxlenX:int = area_mc.x+area_mc.width;//calculate the maximum x area of our clip. I     do this outside the function
// Because the width will change as you move the slider. There are more elegant ways to do this, but I think this is sufficient
// for now. This will be used to stop the slider when it moves all the way to the right of our clip.
function mouseMoveHandler(event:MouseEvent):void {
    var topLeft:Point = area_mc.localToGlobal(new Point(0,0));//Translate the top   left of the clip into global coords.
    var bottomRight:Point = new Point(maxlenX,area_mc.y);//Use the maxlenX variable created above to get the bottom right point. (no need for a point here TBH).
    area_mc.sliderbar_mc.x = area_mc.mouseX;
    if (mouseX <= topLeft.x){//If the mouse is to the left of the top left point...
        area_mc.sliderbar_mc.x = area_mc.globalToLocal(new Point(topLeft.x,topLeft.y)).x;//keep the slider from moving left.
    } else if (mouseX >= bottomRight.x){//if the mouse is to the right of the bottom right point...
        area_mc.sliderbar_mc.x = area_mc.globalToLocal(new Point(bottomRight.x,bottomRight.y)).x;//keep the slider form moving right.
    }
    area_mc.mask_mc.x = area_mc.sliderbar_mc.x;//move the mask.
}