2011-05-17 116 views
0

我有這組驗證碼之前和形象探寶後創建一個簡單的:如何使用此代碼實現globalToLocal?

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功能,可有人請給我一些指針?

+0

你的mouseMoveHandler中的代碼很混亂。你能解釋你想做什麼嗎? – prototypical 2011-05-18 00:47:16

回答

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. 
} 
+0

謝謝你的回覆,它的功能就像一個魅力! localToGlobal是我知道處理這種情況的唯一方式,你會推薦什麼?我試過,但它不是限制:\t功能mouseMoveHandler(事件:MouseEvent)方法:無效{ \t \t area_mc.sliderbar_mc.x = area_mc.mouseX; \t \t VAR左:總數= area_mc.x - (area_mc.width/2); \t \t VAR右:總數= area_mc.x +(area_mc.width/2); \t \t \t 如果\t(area_mc.sliderbar_mc.x>右){ \t \t \t area_mc.sliderbar_mc.x =右; \t \t} \t \t否則如果(area_mc.sliderbar_mc.x <左){ \t \t \t area_mc.sliderbar_mc.x =左; \t \t} \t \t area_mc.mask_mc.x = area_mc.sliderbar_mc.x; \t} – muudles 2011-05-18 04:49:56

+0

您的代碼將不會限制滑塊運動area_mc的範圍內,因爲你動態地調整左,右變量x座標。與行: var left:Number = area_mc.x - (area_mc.width/2); var right:Number = area_mc.x +(area_mc.width/2); 將這兩個變量移出你的函數,就像我用maxlenX做的那樣,滑塊應該被限制在area_mc的內部。 如果我這樣做,我會把整個事情變成一個有自己類的組件,並使用MovieClip的drag()方法。但不應該有任何理由,你不能以你的方式去做。 – Snukus 2011-05-18 07:53:38