A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Call function in Document Class from and included .as

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    16

    Call function in Document Class from and included .as

    I have a document class with functions that I want to access from .as files that are "included" from the timeline of movieclips.

    for example, the Document Class "docClass.as":
    PHP Code:
    package{
        public class 
    docClass extends MovieClip
        
    {
               public function 
    docClass():void
            
    {
                        public function 
    showText(myText):void
                              
    {trace(myText);
                              }
                     }
            }

    and then a separate .as file called from a movieclip within the .fla using

    PHP Code:
    include "TestAS.as"
    and TestAS.as contains:

    PHP Code:
    var txtvar:docClass = new docClass;
    txtvar.ShowText("THIS TEXT WILL NOT TRACE!! PLEASE HELP!!"); 
    What is the correct way to call functions in the Document Class from and included .as file? Thanks,

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You are probably getting an error about no such function ShowText. This is because your method is "showText" with a lowercase 's'.

    If you change that, it should "work", but it's still a terrible design. Instead of creating a new instance of your document class to call showText on, you should use the one you've already got.

    The root is associated with the document class, which means it is an instance of it. You need to get the root, and cast that reference to the document class, then you can call your method.

    docClass(root).showText("whatever");

    I also do not recommend using the include directive. It's not bad per se, but it does fragment your classes into multiple files that make it more difficult to track down code.

    Also also, the common convention is that classes all begin with a capital letter, and everything else begins with a lowercase letter.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    16
    I was kind of hoping to get talked out of using "include".
    I just wanted some way to spread all my code over multiple .as files so as not to clutter my Document class. What's the preferred method for this?
    Also, how do I talk between multiple .as files, in various subdirectories?
    Thanks,

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Instead of having one massive document class that handles everything, create many classes which each have distinct and relatively restricted functionality. Create instances of these new classes and use their methods.

    Don't think about communicating between .as files. You don't have .as files at runtime. You need your class instances to communicate with each other. The only thing that's necessary to do that is for one instance to have a reference to the other instance it wants to communicate with.

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