2016-07-22 63 views
0

我正在開發一個遊戲,AS3的Adobe AIR針對Android和iOS上,中,你必須在中央的影片剪輯,和兩個按鈕(左和右)應該移動該影片剪輯。我的目標是儘可能平滑地在左/右之間切換:
- 如果玩家正在觸摸左按鈕,則影片剪輯向左移動。如果他將手指從左按鈕上移開,並立即觸摸右按鈕,則影片剪輯將不會移動,直到他再次觸摸右按鈕,影片剪輯向右移動。我試圖實現多點觸控事件,但我似乎有什麼問題,因爲這是我得到的行爲。
- 如果玩家觸摸左鍵按預期的影片剪輯移至左側,他摸了右側的按鈕,影片剪輯停止不如預期,但如果他從左路按鈕移除他的手指,同時保持它的權利按鈕,影片剪輯仍然凍結和不動,它應該然後移動到右邊
這是我使用的代碼:
移動電影剪輯,左/右使用觸摸事件與同時觸摸

leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown); 
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown2); 
stage.addChild(leftButtonCreated); 
stage.addChild(rightButtonCreated); 

function mouseDown(e:TouchEvent):void 
     { 

      stage.addEventListener(TouchEvent.TOUCH_END,mouseUp1); 
      //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.; 
      addEventListener(Event.ENTER_FRAME,myButtonClick);//while the mouse is down, run the tick function once every frame as per the project frame rate 
     } 

     function mouseUp1(e:TouchEvent):void 
     { 

      removeEventListener(Event.ENTER_FRAME,myButtonClick);//stop running the tick function every frame now that the mouse is up 
      stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp1); 
     } 

     function mouseDown2(e:TouchEvent):void 
     { 

      stage.addEventListener(TouchEvent.TOUCH_END,mouseUp2); 
      //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.; 
      addEventListener(Event.ENTER_FRAME,stopDragging);//while the mouse is down, run the tick function once every frame as per the project frame rate 
     } 

     function mouseUp2(e:TouchEvent):void 
     { 

      removeEventListener(Event.ENTER_FRAME,stopDragging);//stop running the tick function every frame now that the mouse is up 
      stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp2); 
     } 

    function stopDragging(ev2:Event):void 
    { 

     if (MC.x <= rightButtonCreated.x) 
     { 
      MC.x = MC.x + 10; 
     } 


    } 

    function myButtonClick(ev:Event):void 
    { 


     if (MC.x > leftButtonCreated.x) 
     { 
      MC.x = MC.x - 10; 

     } 
     else 
     { 



     } 

    } 

守則最初設置mouseEvents,所以我試圖轉移到觸摸事件,所以我可以解決這個問題,上面的代碼是我得到的。感謝您的時間。

編輯:

enter image description here

而且,我使用下面的代碼:

var leftButtonCreated:leftB= new leftB(); 

回答

1

您當前的問題是,無論是mouseUp1mouseUp2一旦鬆開左鍵被觸發,如他們都附着在舞臺上。但是你的實際問題更深入。如果按下相應的按鈕,您應該先向左和向右移動對象,然後使用TouchEvent.touchPointID來跟蹤哪個觸摸已釋放,以瞭解哪個按鈕已釋放。

此外還有一個潛在的警告:如果同時觸摸左右按鈕,然後在保持兩個觸摸的同時交換手指,然後釋放位於右側按鈕上方的手指 - 對象應該在哪裏移動,向左還是向右?我說正確的答案在右邊,因爲釋放的手指對應於左邊的按鈕。

leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown); 
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown); 
leftButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp); 
rightButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp); 
addEventListener(Event.ENTER_FRAME,moveObject); 

function touchDown(e:TouchEvent):void { 
    var button:DisplayObject=e.currentTarget as DisplayObject; 
    if (!button) return; // can't fail typecast to DisplayObject in this context, but leave for good measure 
    if (button==leftButtonCreated) { 
     leftButtonPressed=true; 
     leftButtonTouchID=e.touchPointID; 
     return; 
    } 
    if (button==rightButtonCreated) { 
     rightButtonPressed=true; 
     rightButtonTouchID=e.touchPointID; 
     return; 
    } 
} 
function touchUp(e:TouchEvent):void { 
    var button:DisplayObject=e.currentTarget as DisplayObject; 
    if (!button) return; 
    var ti:int; 
    if (button==leftButtonCreated) { 
     ti=leftButtonTouchID; 
     if (ti==e.touchPointID) { 
      leftButtonPressed=false; 
     } 
    } 
    if (button==rightButtonCreated) { 
     ti=rightButtonTouchID; 
     if (ti==e.touchPointID) { 
      rightButtonPressed=false; 
     } 
    } 
} 
function moveObject(e:Event):void { 
    if (leftButtonPressed) MC.x-=10; 
    if (rightButtonPressed) MC.x+=10; 
    if (MC.x<leftButtonCreated.x) MC.x=leftButtonCreated.x; 
    if (MC.x>rightButtonCreated.x) MC.x=rightButtonCreated.x; 
} 

編輯:顯然SimpleButton■不要讓事件外傳播給他們的父母。好的,這個問題仍然可以解決,但是您必須在Main課程中存儲所需的屬性。

var leftButtonPressed:Boolean=false; 
var rightButtonPressed:Boolean=false; 
var leftButtonTouchID:int=0; 
var rightButtonTouchID:int=0; 

上述代碼已經更新。請直接使用SimpleButton s,因爲您使用的是var leftButtonCreated:leftB= new leftB();

+0

我試圖改變我的代碼,並使用一個U建議,但影片剪輯只是停止移動,並在地方 –

+0

我的兩個按鈕是在圖書館凍結,並鑄造,因爲它顯示在編輯以上。 –

+0

將每個按鈕包裝成一個'MovieClip'。像這樣:'var leftButtonCreated:MovieClip = new MovieClip(); leftButtonCreated.addChild(新LEFTB());''SimpleButton'是九流類,不允許多,除了內置的開/關/懸停框的,它實際上實幹家沒有一個時間表(但幀 - 非常怪異) 。 – Vesper