A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: function woes...

  1. #1
    Junior Member
    Join Date
    Oct 2002
    Location
    Vancouver, BC
    Posts
    12

    function woes...

    Hey, everyone. (My very first post!)

    Here's my exact problem. I've written a little actionscript function that creates a new instance of a movie clip (already existing on the stage). Here it is:

    function makeInstance(x, y, square, name) {
    duplicateMovieClip(square, name, 0);
    trace(name.length);
    name._x = x;
    name._y = y;
    }

    ("x" & "y" are the coordinates to stick the instance - each one a copy of whatever "square" is. "name" is whatever name I've fed into the function to name the particular instance.)

    The problem is: it doesn't work. ;-) Any idea why?

    ----

  2. #2
    Senior Member
    Join Date
    Nov 2000
    Location
    Jerusalem, Isreal
    Posts
    254

    ok... pretty simple

    instead of writing:
    name._y = y; // which gives you nothing.

    try writing this:
    _root[name]._y = y;

    that of course if the MC resides on the _root timeLine

    you can also try:
    this[name]._y = y;

    that might work as well.

    good luck.
    Matti Bar-Zeev,
    scriptaholic.

  3. #3
    Senior Member
    Join Date
    Mar 2001
    Posts
    285
    Your function is treating name as a string and not converting it to a target path when you specify the x and y co-odinates. I've improved your syntax and it now works fine:

    function makeInstance(x, y, mc, name, depth) {
    duplicateMovieClip(mc, name, depth);
    this[name]._x = x;
    this[name]._y = y;
    }

    makeInstance(500, 100, "_root.mcSquare", "newSquare",0);

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    name is a string, you have to reference the movieclip WITH that name

    the easiest way to do this here is probably with eval

    function makeInstance(x, y, square, name) {
    duplicateMovieClip(square, name, 0);
    trace(name.length);
    eval(name)._x = x;
    eval(name)._y = y;
    }

    [edit] wow Im slow [/edit]

  5. #5
    Senior Member
    Join Date
    Mar 2001
    Posts
    285
    senocular - Flash MX doesn't like eval being used on the left side of an equation so wouldn't it be better to use the this[name]._x = x syntax instead?

  6. #6
    Junior Member
    Join Date
    Oct 2002
    Location
    Vancouver, BC
    Posts
    12
    Thanks! That seems to work now. SORT of.

    This could be a totally separate problem, but I thought I'd hit you with it anyway. When I call the function more than once, with different x&y locations & different names, it only draws the LAST instance. Do I have to tell the earlier objects to persist or something? Confusing...

    eg.

    makeInstance(20, 50, "one", "firstInstance");
    ...
    makeInstance(20, 150, "two", "lastInstance");

  7. #7
    Senior Member
    Join Date
    Mar 2001
    Posts
    285
    That's why I added the depth argument to your function. If you don't change the depth each time you call your function, you overwrite the previous dupe.

  8. #8
    Junior Member
    Join Date
    Oct 2002
    Location
    Vancouver, BC
    Posts
    12
    hahah Wonderful! I love this stuff!

    Sorry. This is pretty much my first evening with ActionScript. The smallest things really please me!

    Thanks very much for your help - guess I'll go read up on that "depth" stuff...

  9. #9
    Senior Member
    Join Date
    Mar 2001
    Posts
    285
    You're definitely moving in the right direction with your scripting by using functions to execute tasks rather than accessing/changing vars directly. Good luck.

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