-
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?
----
-
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.
-
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);
-
half as fun, double the price
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]
-
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?
-
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");
-
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.
-
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...
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|