【问题标题】:Issue with removing eventlistener in actionscript在 actionscript 中删除 eventlistener 的问题
【发布时间】:2023-03-14 05:01:01
【问题描述】:

我对 flash 并不陌生,但我对 actionscript 有点小白,试图在 flash pro(或者更确切地说,animate cc)中构建一个应用程序,这将(希望)教授用户音乐理论(如何阅读音乐等)。我想要的是在不同的框架上有不同的课程,用户可以在不同的“屏幕”上滑动。我正在使用 adobe 在其滑动库模板中提供的滑动代码的多个副本。

在第 5 帧,我使用以下内容:

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()

第 6 帧几乎相同,只是变量、函数等的名称不同:

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()

还有一个按钮作为正在滑动的影片剪辑“lsn112”的一部分。不知道这是否相关,但代码是:

stop();

fwdtest.addEventListener(MouseEvent.CLICK, GoRootNext112);

function GoRootNext112(event:MouseEvent):void
{
    MovieClip(root).nextFrame();
}

在某种程度上它工作得很好,但我认为事件监听器没有被正确删除。当用户在图库中滑动时,它会按预期工作。然后他们可以移动到下一个画廊,这也可以按预期工作。到目前为止没有错误。但是,如果他们然后返回菜单,然后返回画廊,我会收到错误代码 1009:

TypeError:错误 #1009:无法访问 null 的属性或方法 对象引用。在 MusicTheorySwipe_fla::MainTimeline/slideRightA()[MusicTheorySwipe_fla.MainTimeline::frame6:32] 在 MusicTheorySwipe_fla::MainTimeline/fl_SwipeToGoToNextPreviousFrameA()[MusicTheorySwipe_fla.MainTimeline::frame6:16] 在运行时::ContentPlayer/simulationSendGestureEvent() 在 runtime::SimulatedContentPlayer/clientSocketDataHandler()

让我感到困惑的是,此时我正在使用第 5 帧,但引用第 6 帧时出现错误。在我看来,flash 正在尝试向第 6 帧中的事件侦听器发送手势,即使我是在第 5 帧上,我猜这归因于未删除的事件侦听器。但是,作为代码新手,我不知道何时在不破坏代码的情况下删除事件监听器。

这是一个包含相关 .fla、.swf 和 .xml 文件的 zip 的链接。 http://speedy.sh/5JP7c/MusicTheorySwipe.zip

由于这是我想在很多很多帧上使用的方法,我非常感谢您的时间和帮助解决这个问题。

编辑

好的,我已尽我所能简化代码,以尝试消除任何嫌疑人。

第 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();

第 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();

这就是现在所有的动作脚本,但它仍然无法正常工作。有什么想法吗?

【问题讨论】:

  • 您已经在第 5 帧和第 6 帧上定义了一个侦听器 TransformGestureEvent.GESTURE_SWIPE。一旦到达第 6 帧,两者都将开始执行。一旦你不需要它们,你需要删除它们
  • 是的,我认为这与事件监听器有关,但出于某种原因,我只是专注于错误的监听器。感谢您的提示。

标签: android actionscript-3 flash actionscript animate-cc


【解决方案1】:

在第 2 帧,当您切换到第 6 帧时,检查舞台是否有事件侦听器 fl_SwipeToGoToNextPreviousFrameA(),如果有,请将其删除。这应该可以解决您的错误。

【讨论】:

  • 感谢 Snukus。我测试了一个 removeEventListener,它可以解决错误消息,但是现在当我进入下一帧时,即使代码很好,滑动手势也不起作用。任何想法为什么?我还想使用通过滑动手势移动的影片剪辑中的按钮导航到下一帧或主时间轴上的其他帧。我知道如何使用MovieClip(root).nextFrame(); 执行此操作,但我需要在离开框架之前删除事件侦听器,而且我不知道如何执行此操作,因为无论我尝试什么,我都会得到一个未定义属性的 1120 访问。
  • 您是否同时删除了 SwipeToGoToNextPreviousFrameA 和 B?如果是这样,刷卡将不再起作用。离开第 6 帧时需要删除 A,离开第 5 帧时需要删除 B。
  • Swipe 在第 5 帧上效果很好,但一旦移到第 6 帧,它就不再起作用了。我添加了:if(currentGalleryItem == totalGalleryItems){ stage.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe113); nextFrame() } 因此,如果用户向左滑动,并且没有更多的动画片段可以向左滚动,它会将用户带到下一帧。它有效,我没有收到任何错误报告。但是,在下一帧中,滑动功能不起作用,即使我已经在舞台上添加了一个新的 eventListener。
  • gotoAndStopStop() - 它们会影响脚本的运行吗?我不这么认为,但我的想法已经不多了......
【解决方案2】:

您需要在几帧时删除侦听器。

frame2上写完所有代码

stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);

stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);

在定义监听器之前在frame5上写下这一行

stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);

在定义监听器之前在frame6上写下这一行

stage.removeEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);

并从您可以从第 5 帧和第 6 帧跳转的任何其他帧中删除它们。

【讨论】:

  • 我曾怀疑可能是这种情况,但我只是尝试过,但没有成功。我不知道是 addEventListener 还是 removeEventListener 不起作用,但由于某种原因,它适用于第 1 帧,但不适用于第 2 帧。
  • 你的意思是在第 1 帧工作而不在第 2 帧工作?
  • 抱歉,我的意思是滑动手势适用于第 5 帧,但不适用于第 6 帧。
  • 确保您的所有enterframe也被删除。并在 frame5 listenre 函数上运行跟踪,以便您知道它是否仍在执行。可以在第 5 帧和第 6 帧上跟踪您的所有方法,您就会知道发生了什么。
  • 我没有指定任何输入框。那是我需要解决的问题吗?我知道跟踪功能,但不确定在这种情况下如何使用它。我尝试了trace(stage.EventListener),但出现错误:_Scene 1,Layer 'Actions',Frame 2,Line 30,Column 13 1119: Access of possible undefined property EventListener through an reference with static type flash.display:Stage。 _
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2021-06-20
相关资源
最近更新 更多