6 Replies Latest reply on Feb 12, 2009 8:07 PM by Newsgroup_User

    How to pass data in custom event?

    Handycam Level 1
      I am building a quiz with a question component. When the user clicks one of the choice buttons on the question, it triggers an event to check if the answer was right. I have also created a custom event "ChoiceMade", to tell the parent app that a choice has been made, and have a listener there that sends the user to the next question.

      What I would like to ALSO do is pass not only the event that a choice has been made, but that it was a correct choice, to increment a score. Should I create a separate event "IncrementScore", or do I pass this somehow in the other event?

        • 2. Re: How to pass data in custom event?
          Level 7

          "Handycam" <webforumsuser@macromedia.com> wrote in message
          news:gmsgdp$g90$1@forums.macromedia.com...
          >I am building a quiz with a question component. When the user clicks one
          >of
          > the choice buttons on the question, it triggers an event to check if the
          > answer
          > was right. I have also created a custom event "ChoiceMade", to tell the
          > parent
          > app that a choice has been made, and have a listener there that sends the
          > user
          > to the next question.
          >
          > What I would like to ALSO do is pass not only the event that a choice has
          > been
          > made, but that it was a correct choice, to increment a score. Should I
          > create
          > a separate event "IncrementScore", or do I pass this somehow in the other
          > event?

          There are several ways you can handle this, and the one you have chosen is
          probably the one I would be least likely to select.

          (1) Your parent document should know about the question (the definition
          of the question, not the view component that displays the question), since
          it passed in that information to the view component. One of the things that
          the question should know is which answer is correct. If you have an
          answerChosen property on your question data object, then the application
          should just be able to look at your questions model and see what happened.
          I would be tempted to create a questionManager component that receives these
          types of events directly from the question data objects and handles things
          like incrementing score itself, simply dispatching a "scoreChanged" event
          once it has made the calculation.

          (2) You can create a public property on your view component that
          indicates whether the currently selected choice is correct. When you
          dispatch a "choiceMade", then the application can look at your component's
          property and change the score.

          (3) You can actually make use of the fact that you have made a custom
          event (right now, there is no need for you to have a custom event, and in
          the two scenarios above you wouldn't either
          http://flexdiary.blogspot.com/2007/08/dispatching-events.html). You'd add a
          public property to the event itself that contains that information. If
          that's the tack you want to take, you could look here
          http://www.adobe.com/devnet/flex/articles/graduating_pt2.html, about half
          way down the page.

          I personally don't like (3), because there's no reason here for you to
          create a custom event unless you just want to. If you do use a custom
          event, then you'll have to import its definition both into the parent
          document class and the child document class, bloating your file size and
          coupling them more tightly than necessary.

          MPO is that you'll be happier long term with option 1, because it will give
          you more bang for your buck, especially if you're going to be doing a lot of
          eLearning projects in the future.

          HTH;

          Amy


          • 3. Re: How to pass data in custom event?
            run,ryan! Level 3
            this.addEventListener(ChoiceMade.CHOICE_MADE, function(e:YourEventType){extQuestion(YourParams)});
            • 4. Re: How to pass data in custom event?
              Handycam Level 1
              quote:

              Originally posted by: run,ryan!
              this.addEventListener(ChoiceMade.CHOICE_MADE, function(e:YourEventType){extQuestion(YourParams)});



              Can you please elaborate a bit? Specifically {extQuestion(YourParams)} where is that coming from?
              • 5. How to pass data in custom event?
                Handycam Level 1
                Thanks, Amy, those are good things to evaluate.

                In this instance, I found it was simpler than I thought. I created a public variable at the app level and incremented it:

                • 6. Re: How to pass data in custom event?
                  Level 7
                  "Handycam" <webforumsuser@macromedia.com> wrote in message
                  news:gn2344$irv$1@forums.macromedia.com...
                  > Thanks, Amy, those are good things to evaluate.
                  >
                  > In this instance, I found it was simpler than I thought. I created a
                  > public
                  > variable at the app level and incremented it:
                  >
                  >
                  >
                  > if (e.currentTarget.label == correctAnswer ) {
                  > prompt = "Click to continue";
                  > ttl= "Correct!";
                  > Alert.show(prompt, ttl, Alert.OK, this, alertListener, null, Alert.OK);
                  > Application.application.score += 1;
                  > } else {
                  > prompt = "The answer was \""+getCorrectAnswerText()+"\"";
                  > ttl = "Incorrect";
                  > Alert.show(prompt, ttl, Alert.OK, this, alertListener, null, Alert.OK);
                  > }

                  If you don't mind tightly coupled components, go for it...