View unanswered posts | View active topics



Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
MC from library and classes 
Author Message
Member

Joined: Mon Apr 24, 2006 11:06 am
Posts: 20
Location: Rome, Italy
First of all since this is my first post I would like to thank you guys (developers and forum users) for such a great program and community. it's been a couple of week since I started to use FD and got through the basics by myself and thanks to the posts already on the forum but i feel it's time to start participating (at first with questions then i'll try to help if i can). I am italian so i'f some of my english is unclear just say so and I will try to explain better what i mean.

My question is: i've seen in the swfmill documentation that you can link a PNG to and AS2 class directly (like the Flash IDE does). I haven't found a way to do this in FD and was wondering why... is it because it will be implemented or simply because it is not a good way of coding? I've found a workaround right now by simply attaching the MC at runtime in my main class than create the class and pass it the MC as an argument. I've seen around some classes that help me but i'm trying to do things alone and will use other people's classes when at least i can understand how to do things the best way. So should I wait or is there a better (more adeguate way to code AS) that I can't figure out?


Tue Apr 25, 2006 2:03 pm
Profile
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
Post 
Nick tried to implement it, but I think he ran into issues with the way SWFMILL & MTASC are used currently.

There is however an easy solution: to use a tool class which will handle all the nasty details.
Code:
class Library
{
   /**
   * Attach a library item and assimilate it to a given class
   * @param classRef Wanted class
   * @param target   Parent MovieClip
   * @param id       Library ID
   * @param name     Instance name
   * @param depth     Instance depth
   * @param params    Instance parameters
   * @return Reference to the created object
   */
   static public function attachWithClass(
      classRef:Function,
      target:MovieClip,
      id:String,
      name:String,
      depth:Number,
      params:Object
      ):Object
   {
      var mc:MovieClip = target.attachMovie(id, name, depth, params);
      mc.__proto__ = classRef.prototype;
      classRef.apply(mc);
      return mc;
   }
}


Sample usage:
Code:
var mc:Test = Test( Library.attachWithClass(Test, _root, "library.test.jpg", "test", 1) );


:idea: Feel free to improve this code:
you could remove the id parameter from the method and add it to the class as a static var.


Tue Apr 25, 2006 4:44 pm
Profile WWW
Member

Joined: Mon Apr 24, 2006 11:06 am
Posts: 20
Location: Rome, Italy
Post 
Thanks for the quick reply. if i find a way to extend this code in a way that could be usefull to other's i'll share... how about opening an actionscript section so we could all help each other on our code? anyway tnx again for all you help...


Wed Apr 26, 2006 7:09 pm
Profile
Member

Joined: Tue Feb 21, 2006 5:16 pm
Posts: 8
Post 
hi... i have improved the code with a little feature i need to use

Code:
class Library
{
   /**
   * Attach a library item and assimilate it to a given class
   * @param classRef Wanted class
   * @param target   Parent MovieClip
   * @param id       Library ID
   * @param name     Instance name
   * @param depth     Instance depth
   * @param params    Instance parameters
   * @return Reference to the created object
   */
   static public function attachWithClass(
      classRef:Function,
      target:MovieClip,
      id:String,
      name:String,
      depth:Number,
      params:Object
      ):Object
   {
      var mc:MovieClip = target.attachMovie(id, name, depth, params);
      mc.__proto__ = classRef.prototype;
      classRef.apply(mc);
      return mc;
   }
   /**
   * Creates a Class extends MovieClip on the stage in given context
   * @param   classRef Class to create
   * @param   target Scope where to create
   * @param   name Instance name
   * @param   depth Instance depth
   * @param   params Initialize paramters
   * @return Reference to the created object
   * @author maZe - www.web-specials.net
   */
   static public function createWithClass(
      classRef:Function,
      target:MovieClip,
      name:String,
      depth:Number,
      params:Object
   ):Object
   {
      var mc:MovieClip=target.createEmptyMovieClip(name,depth);
      mc.__proto__ = classRef.prototype;
      if (params != undefined) //Filling with given parameters like attachMovie
      {
         for (var i in params)
         {
            mc[i]=params[i];
         }
      }
      classRef.apply(mc);
      return mc;
   }
}


Last edited by maZe84 on Sun May 21, 2006 6:41 pm, edited 1 time in total.



Sun May 21, 2006 6:34 pm
Profile ICQ
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
Post 
Nice addition :)

I think "createWithClass" would be a better method name.


Sun May 21, 2006 6:39 pm
Profile WWW
Member

Joined: Tue Feb 21, 2006 5:16 pm
Posts: 8
Post 
hmmm.. thanks.. but there is one thing

with using attachMovie with params....

When is the constructor called, after or before the params are set?

cu maZe


Sun May 21, 2006 6:43 pm
Profile ICQ
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
Post 
This line calls the constructor:
Code:
classRef.apply(mc);


Sun May 21, 2006 6:45 pm
Profile WWW
Member

Joined: Tue Feb 21, 2006 5:16 pm
Posts: 8
Post 
yes i know, but i mean the attachMovie in macromedias code?

cu maZe


Sun May 21, 2006 6:47 pm
Profile ICQ
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
Post 
Oh sorry.

It is called after the init parameters have been copied.


Sun May 21, 2006 6:52 pm
Profile WWW
Member

Joined: Mon Jun 12, 2006 8:17 am
Posts: 13
hi there.

is it recommended to use this library class, since it uses the unsupported __proto__ method?

what if adobe[macromedia] remove it in future releases?


Mon Jun 12, 2006 8:53 am
Profile
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
dr.ache wrote:
hi there.

is it recommended to use this library class, since it uses the unsupported __proto__ method?

what if adobe[macromedia] remove it in future releases?

This is a legend, __proto__ is supported.

One can imagine that in AS3 you would not have access to this property, but older SWFs will not break.


Mon Jun 12, 2006 9:09 am
Profile WWW
Member

Joined: Mon Jun 12, 2006 8:17 am
Posts: 13
Post 
thanks alot.

great work on FD, will donate soon


Mon Jun 12, 2006 9:29 am
Profile
Member

Joined: Tue May 16, 2006 11:30 pm
Posts: 46
Post 
i dont understand how to get these classes to work... if i do:

Code:
class testClass extends MovieClip
{
   
   function Land(target:MovieClip)
   {
      
   }
}


Code:
var _test:Test=new Land(Library.createWithClass(testClass,_root,"test",1));


_test still doesnt really extend movieclip...

for example this doesnt work
Code:
_test.lineStyle(3,0,100);
_test.moveTo(0,0);
_test.lineTo(20,20);


Thu Jan 11, 2007 8:17 pm
Profile
Admin

Joined: Wed Aug 31, 2005 7:27 am
Posts: 11602
Location: Paris, France
Post 
Remove 'new' in your code. The createWithClass() method returns a MovieClip reference that you just need to cast.
Code:
var _test:Test = Land(Library.createWithClass(testClass,_root,"test",1));


Thu Jan 11, 2007 9:56 pm
Profile WWW
Member

Joined: Fri Sep 21, 2007 4:57 pm
Posts: 8
Post 
My fav way to create a Class MC is this (you may say that i'm a bit reserved about __proto__, but not too much :twisted: )

1. In my swf lib, there's allways an "empty" asset (a MovieClip exported for actionscript, linkage="empty")

2.
Code:
function attachClassMovie(parent, classRef, name, depth, initObj)
{
    Object.registerClass('empty', classRef);
    parent.attachMovie('empty', name, depth, initObj);
    Object.registerClass('empty', MovieClip);
}



One can set this as a prototype method for MovieClip, but you'll have to trick the compiler to add the intrinsic method.
Code:
MovieClip.prototype.attachClassMovie = function(clsRef, n, d, o)
{
    Object.registerClass('empty', clsRef);
    this.attachMovie('empty', n, d, o);
    Object.registerClass('empty', MovieClip);
}


Fri Sep 21, 2007 10:37 pm
Profile YIM WWW
Display posts from previous:  Sort by  
Reply to topic   [ 22 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.