2017-04-07 111 views
0

有人能幫我把這個代碼從as2轉換成as3嗎?actionscript 2到actionscript 3我的代碼

對於一個簡單的圓形,我希望當我與鼠標光標移動到右邊去,圓旋轉(不需要移動我的鼠標光標但仍一圈旋轉)

我知道_root._xmouse去ŧ mouseX和this._rotationthis.DisplayObject.rotation

onClipEvent(enterFrame) 
{ 
    this.xmouse = Math.min(908, Math.max(0, _root._xmouse)); 
    if (_root._xmouse > 0) 
    { 
     var offset = Stage.width/2 - this.xmouse; 
     this._rotation = this._rotation + offset/2000; 
    } else { 
     this._rotation = this._rotation - 0.02; 
    } 
    this._rotation = this._rotation % 180; 
} 

AS3版本:

stage.addEventListener(Event.ENTER_FRAME, mouseOver); 

function mouseOver(e: Event) : void 

{ 
    rota.mouseX == Math.min(908, Math.max(0, stage.mouseX)); 
    if (stage.mouseX > 0) 
    { 
     var offset = stage.stage.width/2 - rota.mouseX; 
     rota.rotation = rota.rotation + offset/2000; 
    }else{ 
     rota.rotation = rota.rotation - 0.02; 
    } 
    rota.rotation = rota.rotation % 180; 
} 
+0

_「不需要移動我的鼠標光標,但圓形仍在旋轉」_...這就是'EnterFrame'所做的。它以您的SWF的FPS速率重複代碼。也許你想要一個'Mouse_Move'偵聽器中的代碼邏輯?顯示您嘗試製作的AS3版本代碼,更容易幫助您解決問題。 –

+0

是的,也許是一個mouse_move監聽器,這個as2代碼如何工作(as3代碼)...? – romania

+0

我們需要查看迄今爲止您所擁有的AS3代碼,以顯示如何應用as2邏輯。例如:沒有人知道你的圈子變量名等等。你可以用你的Circle變量名稱替換this,並將其用作'circleName.rotation = circleName.rotation%180;'etc etc –

回答

0

這應該工作:

var offset : int = 0; //declare the variable (integer) 

//stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving); 
rota.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving); 

function mouseMoving (evt : Event) : void 
{ 
    rota.x = stage.mouseX; //Math.min(908, Math.max(0, stage.mouseX)); 

    if (stage.mouseX > 0) 
    { 
     offset = stage.stage.width/2 - rota.mouseX; 
     rota.rotation = rota.rotation + offset/2000; 
    }else{ 
     rota.rotation = rota.rotation - 0.02; 
    } 
    rota.rotation = rota.rotation % 180; 
} 

筆記/提示:

  • 聲明的功能如果可能的話外面的變量。

  • evt(evt : Event)是您的目標參考,無論是否有.addEventListener(MouseEvent.MOUSE_MOVE)附加到它。所以如果你想移動多個東西,只需要給他們一樣的addEvent就像rota.addEvent...10一樣,但是正如你所看到的,該功能目前只能移動rota,所以通過改變代碼來使用evt.rotationevt.mouseX etc ... evt現在使其能夠通過通用來監聽mouseMoving功能。


編輯(基於註釋):

旋轉的可變speed套速度。對於rotation請設置方向-=+=

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoving); //Stage : for direction 
rota.addEventListener(Event.ENTER_FRAME, mouseRotating); //Object : for spin/rotate 

var prevX:int = 0; 
var currX:int = 0; 
var directX: int = 0; //will update to 1=Left or 2=Right 

var speed : int = 7; //speed of rotation 

function mouseMoving (evt : Event) : void 
{ 
    prevX = currX; currX = stage.mouseX; 

    if (prevX > currX) { directX = 1; } //moving = LEFT 
    else if (prevX < currX) { directX = 2; } //moving = RIGHT 
    //else { directX = 0;} //moving = NONE 

} 

function mouseRotating (evt : Event) : void 
{ 
    evt.target.x = stage.mouseX; //follow mouse 

    if (directX == 1) { evt.currentTarget.rotation -= speed; } 
    if (directX == 2) { evt.currentTarget.rotation += speed; } 

} 
+0

我喜歡這個謝謝! ,以及我如何做,如果我希望該圈繼續向右移動,如果我去鼠標右側,然後我停止移動鼠標,但圓仍然旋轉到右側(同樣的事情左) – romania

+1

謝謝,但你能**不接受**現在的答案?我試圖讓我的代表得分爲'6-6-6-6'給朋友開玩笑,而'✓'已經把它推得太過分了。你可以在週末或其他時間重新喜歡。 –

+1

完成! ,所以我怎麼能做到我之前說的?如果很簡單.. – romania