2016-09-06 38 views
-1

你好我試圖讓我gotoAndStop按鈕作爲刷卡而不是onpress。但它只能使用一次,第二幀我做不工作了,我不知道該錯誤,請幫助我,謝謝Swipe Gesture,As3

Multitouch.inputMode = MultitouchInputMode.GESTURE; 


story1chapter3.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe); 
function onSwipe (e:TransformGestureEvent):void{ 
if (e.offsetX == 1) { 
//User swiped towards right(back button) 
story1chapter3.x += 100; 
gotoAndStop(31); 
} 
if (e.offsetX == -1) { 
//User swiped towards left(next) 
story1chapter3.x -= 100; 
gotoAndStop(159); 
} 
} 

這個代碼工作,但是當我試圖讓另一個代碼相同,這在不同的幀它不工作了,我也改變了實例名稱,以便它不會dupplicate

Multitouch.inputMode = MultitouchInputMode.GESTURE; 


story1chapter2.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe); 
function onSwipe2 (e:TransformGestureEvent):void{ 
if (e.offsetX == 1) { 
    //User swiped towards right(back button) 
story1chapter2.x += 100; 
gotoAndStop(30); 
} 
if (e.offsetX == -1) { 
    //User swiped towards left(next) 
    story1chapter1.x -= 100; 
    gotoAndStop(27); 
    } 
    } 

PS我也嘗試onSwipe2改變onSwipe,但錯誤出現並稱其重複

回答

0

嗯,這是不乾淨的方式做兩個simila做這個r函數,但這是另一個話題:)我假設問題是,當你第二次刷卡時,兩個事件監聽器都會得到滑動事件,並且都嘗試gotoAndPlay。嘗試刪除在刷卡事件偵聽器:

story1chapter3.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe); 
function onSwipe (e:TransformGestureEvent):void{ 
    if (e.offsetX == 1) { 
    //User swiped towards right(back button) 
    story1chapter3.x += 100; 
    story1chapter3.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe); 
    gotoAndStop(31); 
} 
    if (e.offsetX == -1) { 
    //User swiped towards left(next) 
    story1chapter3.x -= 100; 
    story1chapter3.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe); 
    gotoAndStop(159); 
} 
} 

一個更好的方法將是有在舞臺上只是一個事件監聽器,並有一個功能,將帶給玩家向右章根據您當前所在,那麼你將不需要搞亂幾個功能,但一切都在一個地方

+0

所以如果我讓另一個代碼我可以再次使用onSwipe函數?所以removeEvent的功能是這樣我可以做另一個功能onSwipe?我對嗎?即時通訊對不起,我真的不明白,你可以給我下一個代碼即時通訊,說實際上沒有工作,這是這一個 –

+0

我仍然不能在另一個框架上使用onSwipe函數,它仍然說重複函數定義 –

+0

不,在這個例子中,你仍然需要這兩個功能。 removeEventListener只是告訴第一個函數停止監聽滑動事件,所以只有你的第二個函數會得到它們。在第二個函數中也需要類似的東西:story1chapter2.addEventListener(TransformGestureEvent.GESTURE_SWIPE,onSwipe2);哦,我剛剛注意到,你需要在你的第二個事件監聽器中指定onSwipe2,否則所有的事件都會去onSwipe(這是你現在的主要問題,我認爲):) – Philarmon