如何将 globalToLocal 与此代码结合起来?

发布于 2024-11-08 13:02:11 字数 2466 浏览 0 评论 0原文

我有这套代码来创建一个简单的前后图像揭示者:

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 函数中,有人可以给我一些指示吗?

I have this set of codes to create a simple before and after image revealer:

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();

But now I need to put the revealer area into a movieclip of its own somewhere on the stage, for the life of me I can't figure out how to use globalToLocal to make this work... Here is my attempt:

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();

Obviously it's not working properly, I know it's all in the mouseMoveHandler function, can someone please give me some pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-11-15 13:02:11

好吧,我玩了一下这个,我想我能够让它做你想做的事。它仍然有很多问题需要解决,并且代码很丑陋,但我尝试遵循您尝试使用的方法。为此,我在舞台上创建了所有必要的剪辑。我假设 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.
}

Ok, I played around with this a bit and I think I was able to get it to do what you want. It still has quite a few kinks to work out and the code is ugly but I've tried to keep with the methods you were trying to use. To do this I created all the necessary clips on the stage. I assume area_mc is the clip you're trying to work within and everything else is inside of it.

I might work on it a little more tomorrow, but here it is; tell me if it's doing what you needed:

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文