dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Prototyping in AS3

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    7

    Prototyping in AS3

    Hi,

    Coming from an AS2 background, I used prototypes a lot. I'm having trouble with classes in general, but specifically how to emulate prototyping using AS3. For example, how would I achieve the following using AS3?
    PHP Code:
    Array.prototype.contains = function(element){
        for(var 
    i=0;i<this.length;i++){
            if(
    this[i]==element){
                return 
    true;
            };
        };
        return 
    false;
    }; 
    Secondarily, from what I could get from the help documentation, using a class file is as simple as placing the class code in a .as file with the same name, in the same directory, as a .fla of the same name - however this doesn't seem to work. In AS2, we used "include", but apparently that's not how it's done in AS3. Assuming I have a valid class file in with a .as extension, what's required in the .fla to gain access to it?

    Thanks in advance.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In the OOP world, to achieve that contains functionality, you'd create a subclass of Array with that method, and use instance of that subclass instead of normal Arrays.
    Code:
    public dynamic class MyArray{
      public function MyArray(... values):void{
        super(values);
      }
    
      public function contains(element:*):Boolean{
        if (indexOf(element) != -1){
          return true;
        }
      }
    }
    Code:
    public class SomeOtherClass{
    
       public var myarray:MyArray = new MyArray();
       //...
       if (myarray.contains(4)){
         //...
       }
    }
    Your impression of what is necessary to use a class file is correct. It must be in your classpath (which by default includes the directory the fla is loaded from). That's about it. What specific error are you running into?

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    7
    Hey 5Tons,

    Thanks for the reply. When testing the code sample you provided, I get the following error: 1114: The public attribute can only be used inside a package.

    Can I fix that by adding the following?
    Code:
    package{
    // the sample you posted
    }
    Also, from the looks of it, I'd guess that maybe the "extends Array" bit should be included somewhere?

    Thanks again for you time.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Right on both counts. I shouldn't post so late.
    Code:
    package{
     public dynamic class MyArray extends Array{
       public function MyArray(... values):void{
         super(values);
       }
    
       public function contains(element:*):Boolean{
         if (indexOf(element) != -1){
           return true;
         }
       }
      }
    }

  5. #5
    Junior Member
    Join Date
    Jun 2008
    Posts
    7
    Got it. Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center