11 Replies Latest reply on Feb 4, 2009 6:56 PM by Newsgroup_User

    Problems using a class

    SiHoop Level 1
      I'm having two problems with a class. I want the class to draw some objects (i.e. text and graphics) and return a new property value (i.e. xx) when the graphic is clicked. Problem 1 is that the returned value (xx) is always the last value calculated instead of the value I want to be associated with each instance of the class. How can I get the class to return a unique value when the graphic is clicked? Problem 2 is that I cannot attach the text field (tf0) to the class. I can draw a graphic, and that appears in the correct location, but I don't know how to attach the text field. How should I create and attach the text field within the class. Thanks for your time!
        • 1. Re: Problems using a class
          levancho Level 3
          not sure what you are asking but her eis one obviouse mistake :

          private var tf0:TextField=new TextField

          it should be :
          private var tf0:TextField=new TextField();


          • 2. Problems using a class
            SiHoop Level 1
            I'm trying to do 2 things with the class. I want to add a dynamic text field and I want to create the ability to return a unique value when the user clicks on a graphic. By the way, I believe that both the semi colon and the parentheses are optional and do not affect performance. Thanks for your reply.
            • 3. Re: Problems using a class
              Gregory Lafrance Level 6
              To add the TextField you have to wrap it in a UIComponent, because it is a Flash class, not a Flex class, so it does not descend from UIComponent.

              var myTextField:TextField = new TextField();
              var uic:UIComponent = new UIComponent();
              uic.addChild(myTextField);
              this.addChild(uic);

              How are you setting up a variable for the return value when the graphic is clicked? Is it an instance variable?
              • 4. Re: Problems using a class
                levancho Level 3
                quote:

                Originally posted by: SiHoop
                I'm trying to do 2 things with the class. I want to add a dynamic text field and I want to create the ability to return a unique value when the user clicks on a graphic. By the way, I believe that both the semi colon and the parentheses are optional and do not affect performance. Thanks for your reply.

                you are right I did not realized AS3 allowed that , IMHO that's messed up, from java perspective itis the ugliest way to allow object instantiation without parens ... why parents are optional is there a reason for it?.

                on returning unique value - - are you referring a mouse location when user clicks? (still not clear about differe classes,???)
                anyways, for textFiled issue, maybe try using Label, Text, TextArea, or TextInput controls ?.
                and they should show up when you do: this.addChild


                • 5. Re: Problems using a class
                  Gregory Lafrance Level 6
                  levancho raises a good point. It's best to stick with Flex controls if possible (Label, Text, TextArea, TextInput), rather than TextField, as you will have fewer problems.
                  • 6. Re: Problems using a class
                    SiHoop Level 1
                    Great-- the UIComponent solved the problem. Now my only problem is returning the value. My updated class is listed below. I use the following listener to try to get a unique value associated with each instance, but myInstance.xx returns only the last value assigned to it. I want to have a value assigned for each instance

                    myInstance.addEventListener(MouseEvent.MOUSE_DOWN,doThis)
                    function doThis():void{
                    trace("Position="+myInstance.xx)
                    }
                    • 7. Re: Problems using a class
                      Level 7

                      "SiHoop" <webforumsuser@macromedia.com> wrote in message
                      news:gmciik$krb$1@forums.macromedia.com...
                      > Great-- the UIComponent solved the problem. Now my only problem is
                      > returning
                      > the value. My updated class is listed below. I use the following listener
                      > to
                      > try to get a unique value associated with each instance, but myInstance.xx
                      > returns only the last value assigned to it. I want to have a value
                      > assigned for
                      > each instance
                      >
                      > myInstance.addEventListener(MouseEvent.MOUSE_DOWN,doThis)
                      > function doThis():void{
                      > trace("Position="+myInstance.xx)
                      > }

                      If you actually do create several instances of your component, then each
                      instance will contain its own instance variables, unless you make them
                      static. Possibly you are assigning xx to the wrong instance or myInstance
                      points to a different instance than you think it does. I don't see where
                      you're associating myInstance with the instance in your event handler, and
                      I'd be REALLY surprised if you're not getting an error on your event
                      handler, since addEventListener is going to try to push an event into your
                      function, and your function isn't set up to accept that argument.


                      • 8. Re: Problems using a class
                        SiHoop Level 1
                        I added an argument to my function to accept the event. If I add a trace statement immediately after I create an instance, then the correct value of xx is produced. However, I want that value to be associated with the instance so when I click on the graphic that is part of the instance, that value of xx is produced. I'm pretty sure that my problem is addEventListener. It's only working for the final instance that is created. I've tried adding it to the class, but that doesn't work any better. Here's how I create an instance of the class and (try to) add a listener to the instance:
                        myInstance.addMazes(x,y)
                        myInstance.addEventListener(MouseEvent.MOUSE_DOWN,doThis)
                        function doThis(event:Event):void{
                        trace("Position1="+mazeLayout.xx)
                        }


                        • 9. Re: Problems using a class
                          Level 7

                          "SiHoop" <webforumsuser@macromedia.com> wrote in message
                          news:gmcu6p$59o$1@forums.macromedia.com...
                          >I added an argument to my function to accept the event. If I add a trace
                          > statement immediately after I create an instance, then the correct value
                          > of xx
                          > is produced. However, I want that value to be associated with the instance
                          > so
                          > when I click on the graphic that is part of the instance, that value of xx
                          > is
                          > produced. I'm pretty sure that my problem is addEventListener. It's only
                          > working for the final instance that is created. I've tried adding it to
                          > the
                          > class, but that doesn't work any better. Here's how I create an instance
                          > of the
                          > class and (try to) add a listener to the instance:
                          > myInstance.addMazes(x,y)
                          > myInstance.addEventListener(MouseEvent.MOUSE_DOWN,doThis)
                          > function doThis(event:Event):void{
                          > trace("Position1="+mazeLayout.xx)
                          > }

                          Since you're not changing mazeLayout to the event.target or
                          event.currentTarget, I'm not surprised that it's staying at the value you
                          last set it to, which probably (not coincidentally) is the last one you
                          created!?!


                          • 10. Re: Problems using a class
                            SiHoop Level 1
                            So where should I set the event listener or should I be changing the mazeLayout to the event.target -- although I'm not sure what that means!
                            • 11. Re: Problems using a class
                              Level 7

                              "SiHoop" <webforumsuser@macromedia.com> wrote in message
                              news:gmdj2v$t6g$1@forums.macromedia.com...
                              > So where should I set the event listener or should I be changing the
                              > mazeLayout to the event.target -- although I'm not sure what that means!

                              Come on, now. You've been around long enough not to ask for this kind of
                              handholding. If in doubt, do a search on event.target in the Help.