Topic: As 2.0

AS 2.0/MTASC difficulties.

Hi All,

I'm trying to write my first widget in AS 2.0/MTASC/FlashDevlop.

Below is my two source files.

-------------------Main.as--------------------------------

class Main
{
   
    public static function main(swfRoot:MovieClip):Void
    {
        var swfAPP = new Main(swfRoot);

    }
   
    public function Main(swfRoot:MovieClip)
    {
        var TorrentList:ChumbyList = new ChumbyList(swfRoot, "testString");
       
       
    }
}

-------------------ChumbyList.as--------------------------------
class ChumbyList extends MovieClip
{
    var background:MovieClip;
    var myLongTermString:String;
    public function ChumbyList(myParent:MovieClip, myString:String)
    {
        background = myParent.createEmptyMovieClip("background", myParent.getNextHighestDepth());
       
        myLongTermString = myString;
       
        var BGColour: Number = 0x777777;
        background.beginFill(BGColour, 100);
        background.moveTo(0, 0);
        background.lineTo(100, 0);
        background.lineTo(100, 100);
        background.lineTo(0, 100);
        background.endFill();
       
       
        background.onPress = function() {
            trace("click");
            this.traceString(); //not getting called
            //traceString();    //doesn't compile
           
     }
    }
   
    public function traceString() {
        trace("function called");
        trace(myLongTermString);
    }
}

--------------------------------------------------------------------

As you can see I am trying to call the .onPress function in my ChumbyList constructor.

From within the .onPress function I am unable to call functions from with inside the class. Is there something a particular way to go about this that I am getting wrong?

Thanks in advance,

Joshua

Re: As 2.0

There are a few issues:

1) "this" in the onPress handler would be the MovieClip "background", and therefore the "traceString()" handler is unaccessible since it's method in another class.

2) "ChumbyList" is defined as a subclass of MovieClip, but isn't in reality, since you're not using any of the MovieClip constructors to create it.  That means that "background" can't really access any of its methods without an explicit reference.

Here's one way to resolve these issues:

class ChumbyList // REMOVED extends MovieClip
{
    var background:MovieClip;
    var myLongTermString:String;
    public function ChumbyList(myParent:MovieClip, myString:String) 
    {
        background = myParent.createEmptyMovieClip("background", myParent.getNextHighestDepth());
        
        myLongTermString = myString;
        
        var BGColour: Number = 0x777777;
        background.beginFill(BGColour, 100);
        background.moveTo(0, 0);
        background.lineTo(100, 0);
        background.lineTo(100, 100);
        background.lineTo(0, 100);
        background.endFill();
        
        background.list = this; // ADDED
        background.onPress = function() {
            trace("click");
            this.list.traceString(); //not getting called MODIFIED
            //traceString();    //doesn't compile
            
     }
    }
    
    public function traceString() {
        trace("function called");
        trace(myLongTermString);
    }
}

Re: As 2.0

Thank you for your help. Your solution works great!

What is the best practice for creating custom classes that use movieclips?
Extending movieclip or how it is done above with creating a movieclip inside the class?

If i was have my class extend the movieclip class,
Is there a way to attach it to a parent class? while still being able to pass the constructor the parameters.
e.g
this = myParent.createEmptyMovieClip("background", myParent.getNextHighestDepth());

Cheers

Re: As 2.0

Here's the class we use to do this - I think it originated from FlashDevelop:

class com.chumby.util.MCUtil
{
    //create a movieClip and associate a class with it
    static public function CreateWithClass( classRef:Function,target:MovieClip, name:String, depth:Number, params:Object )
    {
        var mc:MovieClip=target.createEmptyMovieClip(name,depth);
        mc.__proto__ = classRef.prototype;
        if (params != null) for (var i in params) mc[i]=params[i];
        classRef.apply(mc);
        return mc;
    }
    
    //attach a movieClip from the library and associate a class with it
    static public function AttachWithClass( classRef:Function, target:MovieClip, id:String, name:String, depth:Number, params:Object )
    {
        var mc:MovieClip = target.attachMovie(id, name, depth, params);
        mc.__proto__ = classRef.prototype;
        classRef.apply(mc);
        return mc;
    }

    //link a class with an existing movieClip, use for _root / timeline association
    static public function LinkWithClass( classRef:Function, target:MovieClip )
    {
        var mc:MovieClip = target;
        mc.__proto__ = classRef.prototype;
        classRef.apply(mc);
        return target;
    }
}