I'm not new to flash, but I'm a bit of a noob with actionscript, trying to build an app in flash pro (or rather, animate cc) which will (hopefully) teach the users music theory (how to read music, etc.). What I want is to have different lessons on separate frames, with separate "screens" which the user can swipe through. I'm using multiple copies of the swipe code which adobe provides in their swipe gallery template.
On frame 5, I use the following:
stop()
Multitouch.inputMode = MultitouchInputMode.GESTURE;
var currentGalleryItem:Number = 1;
var totalGalleryItems:Number = 10;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);
function fl_SwipeToGoToNextPreviousFrameB(event:TransformGestureEvent):void
{
if(event.offsetX == 1)
{
if(currentGalleryItem > 1){
currentGalleryItem--;
slideRight();
}
}
else if(event.offsetX == -1)
{
if(currentGalleryItem < totalGalleryItems){
currentGalleryItem++;
slideLeft();
}
}
}
var slideCounter:Number = 0;
function slideLeft(){
lsn112.addEventListener("enterFrame", moveGalleryLeft);
}
function slideRight(){
lsn112.addEventListener("enterFrame", moveGalleryRight);
}
function moveGalleryLeft(evt:Event){
lsn112.x -= 128;
slideCounter++;
if(slideCounter == 10){
lsn112.removeEventListener("enterFrame", moveGalleryLeft);
slideCounter = 0;
}
}
function moveGalleryRight(evt:Event){
lsn112.x += 128;
slideCounter++;
if(slideCounter == 10){
lsn112.removeEventListener("enterFrame", moveGalleryRight);
slideCounter = 0;
}
}
Home112.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_22);
function fl_ClickToGoToAndStopAtFrame_22(event:MouseEvent):void
{
gotoAndStop(2);
}
stop()
Frame 6 is almost identical, just with different names for variables, functions, etc.:
stop()
Multitouch.inputMode = MultitouchInputMode.GESTURE;
var currentGalleryItemA:Number = 1;
var totalGalleryItemsA:Number = 11;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);
function fl_SwipeToGoToNextPreviousFrameA(event:TransformGestureEvent):void
{
if(event.offsetX == 1)
{
if(currentGalleryItemA > 1){
currentGalleryItemA--;
slideRightA();
}
}
else if(event.offsetX == -1)
{
if(currentGalleryItemA < totalGalleryItemsA){
currentGalleryItemA++;
slideLeftA();
}
}
}
var slideCounterA:Number = 0;
function slideLeftA(){
lsn113.addEventListener("enterFrame", moveGalleryLeftA);
}
function slideRightA(){
lsn113.addEventListener("enterFrame", moveGalleryRightA);
}
function moveGalleryLeftA(evt:Event){
lsn113.x -= 128;
slideCounterA++;
if(slideCounterA == 10){
lsn113.removeEventListener("enterFrame", moveGalleryLeftA);
slideCounterA = 0;
}
}
function moveGalleryRightA(evt:Event){
lsn113.x += 128;
slideCounterA++;
if(slideCounterA == 10){
lsn113.removeEventListener("enterFrame", moveGalleryRightA);
slideCounterA = 0;
}
}
Home113.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_23);
function fl_ClickToGoToAndStopAtFrame_23(event:MouseEvent):void
{
gotoAndStop(2);
}
stop()
There is also a button as part of the movieclip "lsn112" which is being swiped. Don't know if this is relevant or not, but the code is:
stop();
fwdtest.addEventListener(MouseEvent.CLICK, GoRootNext112);
function GoRootNext112(event:MouseEvent):void
{
MovieClip(root).nextFrame();
}
It works fine to a point, but I think an eventlistener is not being removed properly. When the user swipes through the gallery, it works as expected. They can then move onto the next gallery, which also works as expected. No errors so far. However, if they then go back to the menu, and then back to the gallery, I get an error code 1009:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at MusicTheorySwipe_fla::MainTimeline/slideRightA()[MusicTheorySwipe_fla.MainTimeline::frame6:32] at MusicTheorySwipe_fla::MainTimeline/fl_SwipeToGoToNextPreviousFrameA()[MusicTheorySwipe_fla.MainTimeline::frame6:16] at runtime::ContentPlayer/simulationSendGestureEvent() at runtime::SimulatedContentPlayer/clientSocketDataHandler()
What confuses me is that I am using frame 5 at this point, yet I get an error referencing frame 6. It appears to me that flash is attempting to send a gesture to the eventlistener in frame 6, even though I'm on frame 5, which I'm guessing is down to an eventlistener not being removed. However, being new to code, I don't know when to remove the eventlistener without breaking the code.
Here's a link to a zip containing the relevant .fla, .swf and .xml files. http://speedy.sh/5JP7c/MusicTheorySwipe.zip
As this is the method I would like to use over many, many frames, I would really appreciate your time and help in resolving this.
EDIT
Ok, I've simplified the code as best I can, to try and eliminate any suspects.
Frame 5:
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
var currentGalleryItemA:Number = 1;
var totalGalleryItemsA:Number = 5;
function onSwipeA (e:TransformGestureEvent):void{
//User swiped towards right
if (e.offsetX == 1) {
if(currentGalleryItemA > 1){
currentGalleryItemA--;
lsn113.x += 1280;
}
}
//User swiped towards left
if (e.offsetX == -1) {
if(currentGalleryItemA < totalGalleryItemsA){
currentGalleryItemA++;
lsn113.x -= 1280;
if(currentGalleryItemA == totalGalleryItemsA){
nextFrame()
}
}
}
}
stop();
Frame 6:
stage.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeB);
var currentGalleryItemB:Number = 1;
var totalGalleryItemsB:Number = 11;
function onSwipeB (e:TransformGestureEvent):void{
//User swiped towards right
if (e.offsetX == 1) {
if(currentGalleryItemB > 1){
currentGalleryItemB--;
lsn112.x += 1280;
}
}
//User swiped towards left
if (e.offsetX == -1) {
if(currentGalleryItemB < totalGalleryItemsB){
currentGalleryItemB++;
lsn112.x -= 1280;
}
if(currentGalleryItemB == totalGalleryItemsB){
nextFrame()
}
}
}
stop();
And that's all the actionscript there is now, yet it's still not working. Any ideas?