-1

hello im trying to make my gotoAndStop button as swipe instead of onpress. but it only work once, the second frame i make is not working anymore and i dont know the error please help me , thanks

 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);
} 
}

this code is working but when i try to make another code same as this in different frame its not working anymore, i also change the instance name so it wont 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 i also try to change onSwipe2 to onSwipe , but error comes saying its duplicate

1 Answers1

0

Well this is not the clean way of doing this by having two similar functions but that's another topic :) I assume the problem is that when you do the swipe the second time BOTH event listeners are getting the swipe event and both try to gotoAndPlay. Try to remove the event listener on swipe:

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);
} 
}

A better approach would be to have just ONE event listener on the stage and have one function that would bring the player to the right chapter depending on where you currently are, then you would not need to mess around with several functions but have everything in one place

Philarmon
  • 1,796
  • 1
  • 10
  • 14
  • so if i make another code i can use again the onSwipe function? so the function of removeEvent is so i can make another function onSwipe? am i right? im sorry i dont really understand can you give me the next code im saying that not actually working , which is this one – Jarvis Lorenz D. PAlad Sep 06 '16 at 08:04
  • i still cant use function onSwipe on another frame, its still saying duplicate function definition – Jarvis Lorenz D. PAlad Sep 06 '16 at 08:13
  • No, in this example you still need both functions. The removeEventListener just tells the first function to stop listening to swipe event so only your second function will get them. You will need something similar in your second function as well: story1chapter2.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe2); Oh, i have just noticed that you need to specify onSwipe2 in your second event listener, otherwise all events will go to the onSwipe (which is your main problem at the moment i assume) :) – Philarmon Sep 06 '16 at 09:10
  • Done sir, and yrs thats the problrm. I just changed it all and works thanks so much man . I hope i can give you rate ! Cheers – Jarvis Lorenz D. PAlad Sep 06 '16 at 10:09