A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: creating instance of random library item

  1. #1
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685

    Question creating instance of random library item

    I am trying to add 4 items to stage from the library, "Plane0", "Plane1"..."Plane3". Each uses a base class of "com.Plane", and an auto-generated class for itself (named "Plane0"..."Plane3").

    com.Plane takes 4 parameters.

    I use the following code to get the items on stage:

    PHP Code:
    // getDefinitionById is imported
    for (var i:int 04i++){
        var 
    planeName:String =  "Plane" i;
        var 
    ClassReference:Class = getDefinitionByName(planeName) as Class;
        var 
    myPlane:Plane = new ClassReference("param0","param1","param2","param3");
        
    addChild(myPlane);

    but I get the following error:

    ArgumentError: Error #1063: Argument count mismatch on Plane0$iinit(). Expected 0, got 4.

    what am I missing here?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think that unless you manually add a different constructor to your generated classes, they get a no-argument constructor.
    I'd create an init method that takes your 4 arguments and call that after constructing the new obj.

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    PHP Code:
    for (var i=0i<4i++)
    {
        var 
    _Class:Object getDefinitionByName("Plane") as Class;
        var 
    myPlane:Plane = new _Class();
        
    myPlane.name "param"+i;
        
    myPlane.x=i*90;
        
    addChild (myPlane);

    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    CI, That's not quite what I was trying to do, that the "params" and teh number of loops were the same was a coincidence, what I was trying to do was more like:

    PHP Code:
    for (var i=0i<4i++)
    {
        var 
    _Class:Object getDefinitionByName("Plane") as Class;
        var 
    myPlane:Plane = new _Class();
        
    myPlane.param0 "param0";
        
    myPlane.param1 "param1";
        
    myPlane.param2 "param2";
        
    myPlane.param3 "param3";
        
    myPlane.x=i*90;
        
    addChild (myPlane);

    That's pretty much what I ended up doing, I was just hoping I could pass in my parameters through the constructor... Why? fewer lines of code perhaps. but it works just fine this way, so 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