A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [CS3] Custom Classes And a strange problem...

  1. #1
    ASs 2.0
    Join Date
    Feb 2003
    Location
    Istanbul / Turkey
    Posts
    140

    [CS3] Custom Classes And a strange problem...

    Hi,

    I'm experimenting with custom classes and trying to extend the pre-built classes...

    I found tutorials and followed them and i could successfully wrote my own custom class derived from Math class...

    My question is, i found another custom class from google and tried to integrate that one into my fla using the correct class path. but whenever i test the movie or check the code it gives this error "... class or interface could not be loaded..." see the bold text at the bottom...

    this one is a custom class which extends the MovieClip...


    PHP Code:
    class Rectangle extends MovieClip {

      

      
    // Declare the nested instance
      // as a property.
      
    private var mcShape:MovieClip;

      function 
    Rectangle() {
        
    trace("A Rectangle");
      }

      public function 
    setDimensions(nWidth:Number,
                                    
    nHeight:Number):Void
      
    {
        
        
    // Since you've declared mcShape as a
        // property, you can then reference it.
        
    mcShape._width nWidth;
        
    mcShape._height nHeight;
      }


    a rectangle movieclip with a linkage of myRectangle and class id = Rectangle sits in the library for runtime...

    is there something that i should be aware of with "extending prebuilt classes" approaches ?


    since i can build my own class Makearandom without problems and my classpath settings are ok in the prefs...

    But that particular one only works when the Rectangle.as resides in the same folder with the fla... I ask this just because to have full knowledge of the fundamentals about custom classes...



    ANY IDEAS?

  2. #2
    ASs 2.0
    Join Date
    Feb 2003
    Location
    Istanbul / Turkey
    Posts
    140

    Angry

    I temporarily solved my problem but this time I'm confused about custom classes and class paths thing...

    It really behaves weird... from some of my searches on google different ppl find different reasons or solutions for their own error...

    my first setup was like this...

    CS 3 / AS 2.0

    In my main .FLA in the first frame on the root i have this,

    PHP Code:
    import com.asymbolica.events.*; 
    my AS file named SectionButton.as resides in,

    com/asymbolica/events folder path which is in the same place as my main .FLA

    in my SectionButton AS file the first line reads this way ->

    PHP Code:
    class com.asymbolica.events.SectionButton extends MovieClip 
    (that's also weird that in AS file i have to implement class name like a full path so it works. but having a public function named just as SectionButton. I thought they should be all the way same including letter cases)

    AND FINALLY,

    in my main .FLA i have a movieclip in my library whose class in the linkage properties is set to SectionButton,

    with this setup, whenever i publish my movie it gives the error that i mentioned in the previous post...

    BUT,

    whenever i delete
    PHP Code:
    import com.asymbolica.events.*; 
    and enter the class id in the linkage properties for the MC like this,

    PHP Code:
    com.asymbolica.events.SectionButton 
    voila! it works fine...

    I also just for this project went into the CS 3 preferences and ActionScript tab and set the Flash so it looks also into my cutom class path along with its default paths...

    MY QUESTION IS,

    writing such a long line com.asymbolica.events.SectionButton for each of my MC's in the library isn't the proper and effective way...

    i thought,
    PHP Code:
    import com.asymbolica.events.*; 
    should already do that thing and writing only SectionButton into the class id in the linkage panel would be sufficient...

    but obviously it isn't...

    WHY?

    is it because that the custom plass i have defined in the preferences has a folder which is named with two words with a space between them? like,

    C:/Desktop/Webworks/My Projects/Mywebsite/

    I'm going with trials and errors hence Flash acts weird.

    Yes, in a way i solved my problem but as i said before either i have a misunderstanding about class paths or Flash really lacks of this all classpath, class names thing....

    I hope i'm clear...

    THANKS

  3. #3
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    com.asymbolica.events.SectionButton is not just the Class name, it is the Package reference and Class name all in one. AS3 does things a little better by separating the package and class names.


    As you say, Flash requires you to use the fully qualified path in the library Linkage Properties. That is unfortunately the way it is. There is probably some uninteresting explanation as to why, but I guess you just have to put up with it.

    There is a trick you can use in AS2 though that will enable you to re-cast your MovieClips to another type during runtime. This means that you do not have to assign a class in Linkage Properties as you can deal with it at runtime instead. This is certainly easier than dealing with Class references in the library. (This hack is not possible in AS3.)


    Code:
    class Clip 
    {
    	/**
    	 * 
    	 * @param	scope_obj			: the parent clip
    	 * @param	name_str			: the new clip's name
    	 * @param	depth_int			: the depth
    	 * @param	init_obj			: init Object
    	 * @param	class_obj			: class reference
    	 * @param	class_params_arr	: parameters to pass to the class insance
    	 * @return						: the new clip
    	 */
    	public static function create(scope_obj:Object, name_str:String, depth_int:Number, init_obj:Object, class_obj:Object, class_params_arr:Array):MovieClip
    	{
    		var target_mc:MovieClip = scope_obj.createEmptyMovieClip(name_str, depth_int);
    		if(!target_mc) throw new Error("Clip.create failed");
    		for(var p:String in init_obj) target_mc[p] = init_obj[p];
    		bind(class_obj, target_mc, class_params_arr);
    		return target_mc;
    	}
    	
    	/**
    	 * 
    	 * @param	scope_obj			: the parent clip
    	 * @param	lib_ref_str			: linkage reference
    	 * @param	name_str			: the new clip's name
    	 * @param	depth_int			: the depth
    	 * @param	init_obj			: init Object
    	 * @param	class_obj			: class reference
    	 * @param	class_params_arr	: parameters to pass to the class insance
    	 * @return
    	 */
    	public static function attach(scope_obj:Object, lib_ref_str:String, name_str:String, depth_int:Number, init_obj:Object, class_obj:Object, class_params_arr:Array):MovieClip
    	{
    		var target_mc:MovieClip = scope_obj.attachMovie(lib_ref_str, name_str, depth_int, init_obj);
    		if(!target_mc) throw new Error("Clip.attach failed")
    		bind(class_obj, target_mc, class_params_arr);
    		return target_mc;
    	}
    	
    	/**
    	 * 
    	 * @param	class_obj			: class reference
    	 * @param	target_mc			: target clip
    	 * @param	class_params_arr	: parameters to pass to the class insance
    	 */
    	public static function bind(class_obj:Object, target_mc:MovieClip, class_params_arr:Array):Void
    	{
    		if(!class_obj) throw new Error('Clip.bind requires a class');
    		if(!target_mc) throw new Error('Clip.bind requires a MovieClip');
    		target_mc.__proto__ = class_obj.prototype;
    		class_obj.apply(target_mc, class_params_arr?class_params_arr:[]);
    	}
    }
    example useage...

    Code:
    import Clip;
    import com.asymbolica.events.*;
    
    try
    {
        // attach a clip from library and recast to SectionButton
        var attached_mc = Clip.attach(this, "my_clip", "attached_mc", 0, {_x:50, _y:50}, SectionButton, []);
    
        // or simply recast a clip already on the timeline
        Clip.bind(SectionButton, _mc);
    }
    catch(e:Error)
    {
        trace("Ooops: "+e.toString());
    }
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

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