views:

590

answers:

1

In trying to learn how to create objects in ActionScript, I have had no success. One thing that I have noticed is that there seems to be a billion different ways of doing it. Even if none of them have worked for me. This is really confusing me, and I don't know which approach to try to debug.

The approach that seems to come up most often is:

function myClass() {
   this.val = 1;
}

var test = new myClass();
trace(test.val);

But this just gives me compiler errors no matter how simple I make it, and it makes no sense to me that a function could be an object. If the function is an object, where does "this" point when it is in a function in a function (that is being interpreted as an object).

Another way that seems to come up somewhat less often is:

class myClass {
   function myClass() {
       this.val = 1;
   }
}

var test = .... 

This gives me compiler errors as well, and seems more formal. But I can find very little documentation comparitivley. Most guides are either very simple, or assume that you are talking about the built in objects.

A third way that came up was to create a

 new Object();
in a function, add all the things you wanted and return it. Seems logical. But I have read all sorts of things that mention prototypes, and it seems like an object created this way would be ill suited to be a prototype. But really I am just very confused.

I also came across something saying that Object syntax was different between AS2, and AS3, but nothing more than that.

How am I supposed to create objects? What are the best practices, and for the love of god does somebody know where I can find an in depth tutorial? (my google-fu is evidently weak)

+3  A: 

The class syntax between AS2 and AS3 is indeed different. For AS3 the syntax looks something like the following:

package somePackage {

    public class SomeClass {
     public function SomeClass() {

     }
    }
}

By way of explanation, a package is a wrapper that can contain multiple classes so you have to declare what package contains each class definition.

A class requires a constructor function. In most languages the name of the constructor function is the same as the name of the class. This is the case in ActionScript.

To call an instance of your class would look something like this:

package somePackage {

    public class OtherClass {
     public function OtherClass() {
      var something = new SomeClass();
     }
    }
}

I've got my AS3 reference sitting in front of me so I feel pretty confident with the code snippet above, but unfortunately my AS2 reference is at work but the code should look something like the following:

class ThirdClass {
    public function ThirdClass() {

    }
}

And then to instantiate a variable should look something like this:

var third = new ThirdClass();

Please not that the examples provided do not use strong typing which I would recommend using whenever possible in your ActionScript efforts.

While I would strongly recommend that you pick a copy of Essential ActionScript 2.0 and/or Essential ActionScript 3.0, I have found the following links for beginning tutorials:

Beginning Flash and ActionScript Tutorials

More Beginner Tutorials

Noah Goodrich
I tried this, but for whatever reason, I cannot use it, I have tried putting it in the same file, and putting it in a different file with a variety of import statements. How do i get access to these classes?
Alex
Are you working in AS2 or AS3?
Noah Goodrich
AS3, but I finally managed to get access, I don't know how, i can see nothing different, but it seems to work. Thanks
Alex