2013-05-14 28 views
0

我有一個正在處理的遊戲。我試圖檢測鼠標是否圍繞某個點完成了360度旋轉。儘管如此,我完全在努力。完整的旋轉可以從動態起點向前或向後。確定鼠標是否圍繞某個點做了一個完整的圓形

我有現在的工作:(劍)

所以任何屬性可以被添加,並開始與我有電流相對於點的角度

所以

angleNow =

and

劍。任何屬性都可以添加。我是用像角移動的方向,起始角度等性能瞎搞......

我會後的代碼,但它是一種模糊的,完全不正確。

任何人都可以幫我解決這個問題嗎?僞代碼會很好。希望這是有道理的... ...代碼

if (sword.motionDirection == Const.DIRECTION_POSITIVE) { 

     if (rotateAngle >= sword.motionCurrent) { trace("GOOD" + gameCounter); 
      sword.motionCurrent = rotateAngle; // update the current 
     } 
     else { // switch directions 
      trace("SWITCH" + gameCounter); 
      sword.motionCurrent = rotateAngle; sword.motionDirection = Const.DIRECTION_NEGATIVE; sword.motionStart = rotateAngle; 
     } 
    } 

rotateAngle是角度。

謝謝。

回答

0

另一種可能的解決方案是基於點到圓和鼠標開始的點來劃分舞臺。

您可以通過鼠標和點以及垂直線計算一條直線,以形成舞臺的象限。

在這張圖片中,鼠標是一個正方形,點是一個點。你可以通過找出它進入的象限來判斷鼠標移動的方向。一旦鼠標以正確的順序(1-4或4-1)進入每個象限,就知道鼠標進入第一象限的那一刻,它將會完成一整圈。

實施例:

鼠標進入象限一個(Q1)的第一然後進入Q2,Q3,和Q4然後。此時,只有再次進入Q1,鼠標才能通過它開始的線並完成一整圈。

Image of Mouse, Point, and quadrants

//Import mouse events 
import flash.events.MouseEvent; 
//on whatever event you want throw function to check_for_circle 

    var bound_Q1:Array = [0,90]; 
    var bound_Q2:Array = [90,180]; 
    var bound_Q3:Array = [180,270]; 
    var bound_Q4:Array = [270,0]; 
    var Q1 = false; 
    var Q2 = false; 
    var Q3 = false; 
    var Q4 = false; 


function check_for_circle(event:MouseEvent){ 
    //Calculate angle bounds for each Quadrant 
    //add logic to make sure degrees wrap at 360 
    bound_Q1[0]=//mouse start degree; 
    bound_Q1[1]=bound_Q1[0]+90; 
    bound_Q2[0]=bound_Q1[1]; 
    bound_Q2[1]=bound_Q2[0]+90; 
    bound_Q3[0]=bound_Q2[1]; 
    bound_Q3[1]=bound_Q3[0]+90; 
    bound_Q4[0]=bound_Q3[1]; 
    bound_Q4[1]=bound_Q1[0]; 
} 

if(rotateAngle>bound_Q1[0] && rotateAngle<bound_Q1[1]/*mouse is in Q1*/){ 
    if(Q1 && Q2 && Q3 && Q4){ 
    //full circle! 
    }else if(Q1 && Q2){ 
    //This means we turned around 
    Q4=false; 
    Q3=false; 
    Q2=false; 
    } 
    //we are in Q1 
    Q1=true; 
} 

if(rotateAngle>bound_Q2[0] && rotateAngle<bound_Q2[1]/*mouse is in Q2*/){ 
    if(Q1 && Q2 && Q3 && Q4){ 
    //we started at Q4 and turned around 
    Q1=false; 
    }else if(Q1 && Q2 && Q3){ 
    //We started at Q1 and turned around 
    Q3=false; 
    Q4=false; 
    } 
    //we are in Q2 
    Q2=true; 
} 

if(rotateAngle>bound_Q3[0] && rotateAngle<bound_Q3[1]/*mouse is in Q3*/){ 
    if(Q1 && Q2 && Q3 && Q4){ 
    //we started at Q1 and turned around 
    Q4=false; 
    }else if(Q4 && Q2 && Q3){ 
    //We started at Q4 and turned around 
    Q2=false; 
    Q1=false; 
    } 
    //we are in Q3 
    Q3=true; 
} 
if(rotateAngle>bound_Q4[0] && rotateAngle<bound_Q4[1]/*mouse is in Q4*/){ 
    if(Q1 && Q2 && Q3 && Q4){ 
    //full circle! 
    }else if(Q4 && Q3){ 
    //This means we turned around 
    Q1=false; 
    Q3=false; 
    Q2=false; 
    } 
    //we are in Q4 
    Q4=true; 
} 
0

如果鼠標在同一個地方開始並結束,則鼠標在該點周圍形成一個完整的圓。如果我們嚴格遵循這個定義,它會在遊戲中造成一些問題,因爲玩家不需要完成圈子來拋出任何圈子完成事件。

但是如果你做一個命中測試用鼠標所在的座標鼠標開始,它退出某個方向,當它在一定方向進入,你將能夠避免這樣的問題。

由於這是一個混亂的話,幾乎沒有任何意義,我下面有一個鮮豔的顏色圖。

讓我們假裝兩件事。首先,藍點是我們想要限定的點。其次,我們只想在點擊後進行我們的圈子測試。

當我們點擊我們周圍的藍點的某一點,會出現一個命中測試區域。它是由三個獨立的可測試領域,我們姑且稱之爲left_exit(作爲下綠色元素可見),right_exit(如上部綠色元素可見),並center(如紅色元素可見)。

命中測試區域出現從而center是鼠標之下,從舞臺到藍點的邊緣一直延伸。

當鼠標停留在center,發生什麼。

然而,當鼠標移出中心有三種可能出現次數:

  1. 鼠標未能以循環的方式移動,沒有擊中任何綠地

    在這種情況下,我們知道鼠標未能圍成一圈,我們可以取消點擊測試。

  2. 鼠標移動到上部的綠色元素right_exit

    在這種情況下,我們知道鼠標正在向特定方向移動,爲了保存該方向,我們可以設置一個布爾值(可能被稱爲hit_right)爲true。

  3. 鼠標移動到較低的綠色元素left_exit

    在這種情況下,我們也知道鼠標正在向特定方向移動,我們可以通過設置布爾值(可能稱爲hit_left)爲true來保存該方向。

在情況2和3有兩個可能的選項:

  1. 鼠標返回到紅色元件center

    在這種情況下,它們不再沿特定方向移動,因此我們可以將某些布爾值設置爲false。

  2. 鼠標退出元素並轉到相反的綠色元素。

    在這種情況下,圓幾乎完成。我們可以將另一個布爾值設置爲true。

情況下2之後,有兩種可能的選擇:

  1. 鼠標離開錯誤的方向這個綠色元素。

    含義圓仍不完整。

  2. 鼠標進入紅色元素center

    含義我們已經完成了一個圓圈。

Mouse Hit Test Circle

//Import mouse events 
import flash.events.MouseEvent; 
//on mouse event MOUSE_DOWN throw function check_for_circle 
stage.addEventListener(MouseEvent.MOUSE_DOWN, check_for_circle); 

    var hit_right = false; 
    var hit_left = false; 

function check_for_circle(event:MouseEvent){ 
    //draw invisible hit-test 
    //make sure that center forms a line from the 
    //point, through the mouse, and to the edge of the stage 
} 

right_exit.addEventListener(MouseEvent.MOUSE_OVER, check_right); 
left_exit.addEventListener(MouseEvent.MOUSE_OVER, check_left); 
center.addEventListener(MouseEvent.MOUSE_OVER, check_center); 
//add MOUSE_OUT checks as well 
//and if you are scared that people will try to cheat, 
//add a MOUSE_LEAVE function to the stage 

function check_right(event:MouseEvent){ 
    hit_right = true; 
} 
function check_left(event:MouseEvent){ 
    hit_left = true; 
} 
function check_center(event:MouseEvent){ 
    if(hit_left && hit_right){ 
    //yay it worked! 
    } 
    else{ 
    hit_left = false; 
    hit_right = false; 
} 

我希望這有助於!

+0

謝謝,我參加了一個裂縫它,並有以下它的一些麻煩。我認爲我有主要想法,就像在影片剪輯中提到的那樣,並確保鼠標按照正確的順序進行測試。 雖然,我正在尋找一個可能的簡單解決方案。我在邏輯上仍然存在一些缺陷,但至少在正面方向上工作正常。我也擺脫了一個動態的「起點」,以簡化它。 – 2013-05-14 20:48:56

+0

我不能爲我的生活弄清楚如何發佈代碼,但這裏是未知的: '\t if(rotateAngle> lastAngle)lastAngle = rotateAngle; \t否則如果(rotateAngle 350 && rotateAngle> = 0){ \t \t \t跡( 「PossibleCircle&重置」 +計數); \t \t \t lastAngle = 0; \t \t} \t} \t否則{ \t \t跡( 「elsed」); \t} ' 它可以在正方向上移動,但如果向後移動,它可能會使鼠標移動。 角度rotateAngle是相對於其盤旋的點和鼠標。我真的只是想看看鼠標是否在所有點上移動。 – 2013-05-14 21:03:52

+0

如果我不能以這種方式工作,我會再看看你的方式,謝謝你的完整例子。 – 2013-05-14 21:05:23