14 Replies Latest reply on Jan 16, 2008 5:37 AM by Greg Dove

    How to call a function inside a class? 

    chrisatflash Level 1
      Hello, i am trying to fire a function onPress .. this function is inside a class.
      In this function i refer to another function inside the calss to call a Tween. It never arrives in the second function.

      I tried to make an example.

      In the fla file i have:
      var test:makeMovie = new makeMovie(this);


      You will see a red squere and you can press on it. It should run the tween class. Am i using the delegate class wrong?

        • 1. Re: How to call a function inside a class? 
          clbeech Level 3
          I believe that the Delegate class is more for use 'outside' the class to specify a scope within a class object, but not 'within' the class itself. However I am uncertain, not having used the Delegate class myself.

          But you should be able to assign the method directly within the class, as in:

          Container.onPress = setTween;
          • 2. Re: How to call a function inside a class? 
            kglad Adobe Community Professional & MVP
            that's cheating (though he'll need to use "this" instead of Container in that tween).

            what if he wants to execute two class functions when Container is pressed?
            • 3. Re: How to call a function inside a class? 
              clbeech Level 3
              LOL!! yeah, probably kglad :)

              would it be correct to use the Delegate class here? I guess i would probably write it like a normal handler method and call two other methods? what's the proper way?
              • 4. Re: How to call a function inside a class? 
                kglad Adobe Community Professional & MVP
                i don't know a clean way to do it.

                inside the setAlpha function, you're in the scope of Container (not the class). so, you can't access any class methods or properties from setAlpha when it's called like that.

                you can add a property to Container so it can reference its own class and you can use that to get within the class scope inside setAlpha. but then the compiler will complain because it thinks you're trying to use a class property and it sees none.

                the only way i could find to stop the compiler from complaining was to create a dummy class property so the compiler wouldn't complain.

                that works but it's not an elegant solution.
                • 5. Re: How to call a function inside a class? 
                  Greg Dove Level 4
                  I always have to use test movie and trace a couple of times each time I start to use Delegate.
                  It wraps function.apply I think, which I tend to use more often.

                  try using:

                  Container.onPress = Delegate.create(this,setAlpha);

                  and then just
                  setTween()
                  inside your setAlpha....

                  That's conceptually how I think I would try it above if I was doing something similar.

                  I'm not sure it would have the desired effect even if the delegate was executed as you have it coded. Unless you want setTween's execution scope to be the Container clip in which case you might need to do what kglad said and change the Container reference to 'this'.


                  The way you have it at the moment inside setAlpha the Delegate.create is simply creating a function...and not excuting it.

                  its like : function something(){trace('what')}

                  and not like : something();

                  Below is some quick code that helps show how things work. Notice that I assigned the delegate function to mainFunc....so that along with the last example might provide a clue. Just paste it on a frame and take a look at what's happening.

                  • 6. Re: How to call a function inside a class? 
                    chrisatflash Level 1
                    Thanks for the explenations. I tried to modify it, but now i can not aproach the Container movieclip.
                    I don't understand how to make a reference to this Container. I tried for two hours now.. but i give up.

                    This is the code i have so far.

                    And in my fla file i still have:

                    var test:makeMovie = new makeMovie(this);


                    • 7. Re: How to call a function inside a class? 
                      Greg Dove Level 4
                      is the trace 'start tweening?' showing?

                      if so, then change it back from
                      new Tween(this, "_ ....etc

                      to
                      new Tween(Container, "_ ....etc

                      see if that works
                      • 8. Re: How to call a function inside a class? 
                        Greg Dove Level 4
                        Oh and you will need to change this line:


                        var Container:MovieClip = parentMc.createEmptyMovieClip("container_MC", parentMc.getNextHighestDepth());

                        to this:

                        Container = parentMc.createEmptyMovieClip("container_MC", parentMc.getNextHighestDepth());
                        • 9. Re: How to call a function inside a class? 
                          Greg Dove Level 4
                          And one last thing.... there's no need for your class to extend MovieClip. It only creates them unless I've misunderstood your intentions with the class. So you could remove extends MovieClip.
                          • 10. How to call a function inside a class? 
                            chrisatflash Level 1
                            I tried your suggestions, but nothing happend.

                            There was only one way that kind of worked... (see my attached code).

                            as you can see in line 27, that there will not be listened to the onPress event because i tried
                            to pass a value in the delegate class using () .. and when you use this () the function gets fired immediately.

                            I also tried to do this:

                            but this way it did not woked :(

                            Container.onPress = function() {
                            Delegate.create(this,setAlpha(Container));
                            }
                            • 11. Re: How to call a function inside a class? 
                              Greg Dove Level 4
                              Either my instructions weren't clear or you misinterpreted them. It works fine with my suggestions as follows:

                              And with this on the main timeline:

                              import makeMovie;

                              var mymovie=new makeMovie(this)

                              • 12. Re: How to call a function inside a class? 
                                Greg Dove Level 4
                                Of course the setAlpha function is not really doing anything there except calling the other function/method...but I was more illustrating the point of how to get it working... I'll leave it up to you how you change that (e.g. you could just have the Delegate function set up for setTween directly)
                                • 13. Re: How to call a function inside a class? 
                                  chrisatflash Level 1
                                  thanks! you example works. And if i would like to run more functions 'onPress' then i have made
                                  another function. This way you can fire multiple functions 'onPress'.

                                  I guess this is the basic to start building some OOP things in flash. I will start from here. :)

                                  • 14. Re: How to call a function inside a class? 
                                    Greg Dove Level 4
                                    You're welcome. That's just one way of approaching it. You could make a class that extends MovieClip and associate it a with a library symbol and instantiate it with attachMovie. I think that's probably the more usual method. I guess either approach is fine and it depends on what your preferences and objectives are.

                                    Good luck with it all!