Yunnan
Yunnan

Reputation: 79

Call to a possibly undefined method '' through a reference with static type Class

I made small .fla file in Flash Professional, and I have added .as (ActionScript File) in Flash Professional, and I have added something like code below to .as (ActionScript file), but the error appears and I am trying to figure it out, but can't, so I decided to post it in here instead.

package
{
    import flash.display.MovieClip;

    public class Bag extends MovieClip
    {
        static var firstBag:String;

        public static function set setFirstBag(value:String):void
        {
            firstBag = value;
        }

        public static function get getFirstBag():String
        {
            return firstBag;
        }
    }
}

and I called it like this:

button1.addEventListener(MouseEvent.CLICK, onClickFirstButton);

function onClickFirstButton(e:MouseEvent):void
{
   Bag.setFirstBag("First slot in the bag has been filled up!");
}

But I have received this following error:

Call to a possibly undefined method setFirstBag through a reference with static type Class.

What could I do wrong?

The .as file and .fla file are on the same folder.

if I changed the Bag class to static. The error will be like this:

The static attribute may be used only on definitions inside a class.

Your answer much appreciated!

Thank you!

Upvotes: 0

Views: 957

Answers (2)

Aaron Beall
Aaron Beall

Reputation: 52143

A few additional thoughts...

While syntactically valid, the definition and naming of your getter and setter is confusing and atypical, which I think contributed to your confusion about the behavior. You've actually defined two separate properties, one is write-only ("setFirstBag") and one is read-only ("getFirstBag"). Usually you define a getter/setter as the same property (ex "firstBag"), and without any "get" or "set" in the property name, since that is what the getter/setter is defining for you. Example:

private static var _firstBag:String;
public static function get firstBag():String {
    return _firstBag:
}
public static function set firstBag(value:String):void {
    _firstBag = value;
}

// usage
Bag.firstBag = "stuff";
trace(Bag.firstBag); // "stuff"

Also, you may very well have a good reason to use a getter/setter here, or you might just prefer it, but from the code you posted you could just define a public static var to do the same thing. (If you did, refactoring into a getter/setter if you needed some side-effect logic would be trivial, since the public API remains the same.)

Upvotes: 0

Azzy Elvul
Azzy Elvul

Reputation: 1438

You're useing get like it is a mettod, but thay are accessors for properties so intead of:

Bag.setFirstBag("First slot in the bag has been filled up!");

use

Bag.setFirstBag ="First slot in the bag has been filled up!";

Upvotes: 2

Related Questions