Skip navigation
Currently Being Moderated

I have a problem with an app AS 3.0

Aug 28, 2012 10:36 AM

Hi, my name is Bryan, and I got to say that I am new to actionscript 3

Yesterday I learned a little bit more about it, Straight to the point, I am making an iOS app using Flash CS6

and everything seems right,

 

I have my codes correctly, and my back and next buttons stop working after I go back to the previous scene

Ok, lets explain better, my app starts with an introduction, bla bla bla, after there's a menu with options,

i hit the first "Chapter in this case" and goes to a new frame, i hit next next next etc. with no problem,

but when i am back to the "chapter" menu  and hit the first chapter again the next button stop working need some help

 

here are my btns codes:

 

 

 

import flash.events.MouseEvent;

 

 

stop();

// EventListeners

 

 

back_btn.addEventListener(MouseEvent.CLICK, backFunc);

menu_btn.addEventListener(MouseEvent.CLICK, menuFunc);

next_btn.addEventListener(MouseEvent.CLICK, nextFunc);

 

 

// Functions

 

 

function backFunc(evt:MouseEvent) :void {

                    prevFrame();

}

function nextFunc(e:MouseEvent){

          nextFrame();

}

function menuFunc(a:MouseEvent) :void {

          gotoAndStop(3);

}

 

I Hope i can get some help

 
Replies
  • Currently Being Moderated
    Aug 28, 2012 10:56 AM   in reply to iden_tidad

    If this code is on frame 1 and you allow your "prev" button to return back to that frame it will re-assign all the buttons again and again. You should definitely init your app on frame 1 but then disallow the app to return to frame 1. So push all your content over to frame 2+ and never return to frame 1.

     

    Otherwise you can set a flag for yourself to know if you need to assign the button handlers. If it's the first time you run the app you can check for the existence of a variable. If it doesn't exist then set it and init your buttons.

     

    e.g. Frame 1:

     

    if (!applicationAlreadyStarted)

    {

         // add event listeners

     

         var applicationAlreadyStarted:Boolean = true;

    }

    Then the next time it hits that frame the variable will exist and you won't accidently re-assign all your buttons. Add any other init code in there that should only fire off once when your app is started. It might be what's causing trouble.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 6:08 AM   in reply to iden_tidad

    In your case I think the second method I mentioned may just be easier for you so you don't need to affect the timeline. However I can't recommend that unless I know what your timeline looks like.

     

    Before continuing can you tell me of the buttons "back_btn", "next_btn" and "menu_btn" exist across the entire timeline without any keyframes breaks between them? If so the layer would look solid, similar to this where you see different content keyframes on all 10 frames but the buttons layer has no keyframes past frame 1:

     

    10framecontent1.JPG

     

    As you can see the buttons layer has one long gray keyframe. Therefore any code inserted in frame 1 will continue to exist on the buttons all the way up to frame 10.

     

    However if you insert a keyframe on the buttons layer, flash will "re-create" those buttons. Therefore they will not have any code applied to them unless you specifically apply it yourself on that frame. An example of that looks like this:

     

    10framecontent2.JPG

     

    That would require you to re-apply your mouse click listeners every single frame to assure they work.

     

    This just a quick example not to be taken too literally but I need to know what your timeline looks like to make the best suggestion.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 6:48 AM   in reply to iden_tidad

    You can make it as simple as:

     

    function backFunc(evt:MouseEvent):void

    {

         if (this.currentFrame > 1)

         {

              prevFrame();

         }

    }

     

    If the frame isn't "greater than 1" then it will simply ignore the click. Use the same logic in nextFunc and see if you can write it so if this.currentFrame < 6 it will fire off nextFrame();.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 7:13 AM   in reply to iden_tidad

    Oy, forum error. I just posted a function on how to do that. I'll retype it quickly..

    function backFunc(evt:MouseEvent):void

    {

         var invalidFrameNumbers:Array = new Array(2, 5, 7, 11);

         var validPress:Boolean = true;

     

         for (var i:int = 0; i < invalidFrameNumbers.length; i++)

         {

              if (invalidFrameNumbers[i] == this.currentFrame)

              {

                   // matched an invalid frame number, set flag, break loop

                   validPress = false;

                   break;

              }

         }

     

         // check if it was a valid press

         if (validPress)

         {

              prevFrame();

         }

    }

     

    There are other ways to do this like searching arrays but this illustrates some basics you definitely want to get down. Things like Arrays, Boolean flags and looping over data.

     

    This makes an array of invalid frames and sets a flag. It loops over all the items in the array and if any of the numbers match the current frame number it sets the flag to false and breaks the loop. After the loop it checks to see if the flag states the press is valid and only then will it run prevFrame().

     

    You'd use similar logic for the next_btn.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 8:10 AM   in reply to iden_tidad

    Often you need to look inside code to see what's going on. That code looks fine to me. I re-wrote it but added some trace() statements. When you press your next_btn you should see the messages I'm trace()ing. See if anything unusual is happening.

     

    function nextFunc(e:MouseEvent):void

    {

        var invalidFrames:Array = new Array(5, 11);

        var validPress:Boolean = true;

       

        for (var i:int = 0; i < invalidFrames.length; i++)

        {

            trace('Checking if invalidFrame[' + invalidFrames[i] + '] == [' + this.currentFrame + ']');

            if (invalidFrames[i] == this.currentFrame)

            {

                trace('Invalid frame matched, press invalid');

                validPress = false;

                break;

            }

        }

       

        if (validPress)

        {

            trace('No invalid frame detected, running nextFrame()');

            nextFrame();

        }

    }

     

    Your trace output can be found in the output panel. Say you're on frame 11, you should see something like this:

    Checking if invalidFrame[5] == [11]

    Checking if invalidFrame[11] == [11]

    Invalid frame matched, press invalid

     

    If you were on frame 1 then it should look like:

    Checking if invalidFrame[5] == [1]

    Checking if invalidFrame[11] == [1]

    No invalid frame detected, running nextFrame()

     

    Use trace() statements as much as possible along with your debug panel to track down logic issues.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 8:30 AM   in reply to iden_tidad

    The code tends to be right. Might want to run your play head across the timeline and take a look at the frame number you think is wrong and I bet you'll see it's right. For example the menu you might think is appearing on frame 5 actually appears on frame 4, or vice versa.

     

    Glad you got it working. If you're all set please mark it answered so we can attend unanswered questions and good luck!

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 8:43 AM   in reply to iden_tidad

    You're welcome and please mark it answered so us forum hounds can filter out answered questions. Good luck!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points