Skip to main content
sbryner
Inspiring
June 15, 2009
Question

Class call a method

  • June 15, 2009
  • 2 replies
  • 1396 views

Hi,

If you have numerous methods in a class, can you just call them like so:

// in the .fla file

var john:Person = new Person(63,150); // creates a new person named john 63" and 150lbs.

john.weight(180); // changes johns weight to 180lbs.

// in the .as file

package {


    import flash.display.MovieClip;

    public class Person extends MovieClip {


        public var _perHeight:Number;
        public var _weight:Number;

      public function Person(perHeight:Number, weight:Number) {

            _perHeight=perHeight;

            _weight=weight;

            this.perHeight=_perHeight;
            this.weight=_weight;

        }


        public function changeWeight(newWeight) {
            this.weight=newWeight;
        }


    }

}

I must be calling the method completely wrong? I get this error:

1061: Call to a possibly undefined method weight through a reference with static type Person.

This topic has been closed for replies.

2 replies

Inspiring
June 15, 2009

That is because the method is called "changeWeight" and you are using "weight."

But what you really want is probably not a method but rather a getter/setter that acts like a property -- or maybe you just want a property.

So in your class you would have this:

package {


    import flash.display.MovieClip;

    public class Person extends MovieClip {


        private var _perHeight:Number;
        private var _weight:Number;

        private var _age:Number;

        public function Person(perHeight:Number, weight:Number) {

            _perHeight=perHeight;

            _weight=weight;

        }


        public function set weight(num:Number):void{

            _weight=num;

        }

       public function get weight():Number{

          return _weight;

       }

    }

}

Then in your code (after creating a person instance):

john.weight=180;// changes john's weight to 180.

The cool thing about getters and setters is that they act just like a property when you use them, but they allow for a whole function call at the other end and can do various things based on the inputs.

Also variable that start with an underscore are usually private variables. So in this case _weight, _age, and _perHeight would be private -- you couldn't access them directly through the john instance for example. And you would have appropriate getters and setters for them. For example you might make an age setter that didn't allow the number to be set as smaller than the current value or something.

sbryner
sbrynerAuthor
Inspiring
June 16, 2009

Thanks both of you guys.

Rothrock, your answer very well explained. I appreciate you both helping to figure this out.

So,

"_variable" underscored variables are most commonly used as private functions, why is that? just to keep code cleanly formated?

When you call a method, using a getter/setter

john.weight=180;

Is it calling the getter or setter first?

Does the first, say it was "set weight()" (in this example) would it automatically call the get weight() afterwards to return the value to john?

Thanks for the explainations!

sbryner
sbrynerAuthor
Inspiring
June 16, 2009

Also, with the changed code it comes up as an error:

"1195: Attempted access of inaccessible method weight through a reference with static type Person.

Ned Murphy
Legend
June 15, 2009

The method would be changeWeight, wouldn't it?  And you would be assigning that new weight to the _weight variable, wouldn't you?

john.changeWeight(180);

And you should define the type in the function argument

public function changeWeight(newWeight:Number):void {

...

}