A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: weirdness when trying to use attachMovie a class

  1. #1
    Senior Member
    Join Date
    Sep 2002
    Location
    Clogland
    Posts
    100

    weirdness when trying to use attachMovie a class

    Actionscript 1.0

    I'm attaching a movie made at runtime ("box_mc").

    This works....

    code:

    function Box(x_pos,y_pos,dep) {
    attachMovie("box_mc","aName", dep);
    aName._x = x_pos;
    aName._y = y_pos;
    }
    bla2 = new Box(50,50,2);
    bla1 = new Box(100,100,1);



    This doesn't (level property "dep" lower on first attach), the x_pos and y_pos aren't recognised.

    code:

    function Box(x_pos,y_pos,dep) {
    attachMovie("box_mc","aName", dep);
    aName._x = x_pos;
    aName._y = y_pos;
    }
    bla1 = new Box(50,50,1); // changed
    bla2 = new Box(100,100,2); // or swapped



    Am I missing something basic here?

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    You're creating the clip using the same name, so once there is more than 1 aName flash has no way to know which one of these clips you meant to refer to when you use something like aName._x = x

    maybe use something like this instead,

    var mc = attachMovie("box_mc","aName" + dep, dep);
    mc._x = x_pos;
    mc._y = y_pos;

    Is there are reason to use attachMovie in the a constructor function? if you're creating a movie clip subclass this might be better,

    code:

    function Box() {
    // this.params is created by the initObj argument in the attachMovie call
    this._x = this.params.x;
    this._y = this.params.y;
    // clean up the params object now it has done its job
    delete this.params;
    }
    // inherit from MovieClip
    Box.prototype = new MovieClip();
    /*
    define other methods for the Box class here, eg

    Box.prototype.methodName = function() {
    // do stuff
    };

    */

    // set up so that clips created from the clip with the linkage id box_mc will be instances of the Box class
    Object.registerClass("box_mc", Box);

    // now create a couple of boxes
    var bla1 = this.attachMovie("box_mc", "box1", 1, {params: {x: 50, y: 50}});
    var bla2 = this.attachMovie("box_mc", "box2", 2, {params: {x: 100, y: 100}});


  3. #3
    Senior Member
    Join Date
    Sep 2002
    Location
    Clogland
    Posts
    100
    Originally posted by catbert303
    maybe use something like this instead,

    var mc = attachMovie("box_mc","aName" + dep, dep);
    mc._x = x_pos;
    mc._y = y_pos;
    Thank you kindly, adding the "var mc =" was the magical solution.

    Is there are reason to use attachMovie in the a constructor function?
    Er, inexperience?

    Once I get the thing working totally as I foresaw it I will be taking a look at the code you posted to try and work out it's inherent advantages.

    Thanks for taking the time to try and help me take my scripting skills to a higher level.

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