Gunto,
That's a good start, but depending on how you use this function, you
might run into some issues on exactly where the line is drawn. The reason
for this is that "this" keyword, which refers to the current scope in which
it appears. In this case, you've got the "this" reference inside a custom
function, which means it points to the scope of this function. The
function, itself, is an instance of the Function class, which doesn't
feature a createEmptyMovieClip() method. When the function can't find that
method inside itself, it looks to the next available scope, which is
probably the timeline that contains the code you've typed.
Depending on your needs, this might work fine. But you might also want
to pass in a timeline parameter, so you can determine with confidence which
movie clip (aka timeline) receives the newly created movie clip container.
Something like this, maybe:
function drawline(x0, y0, x1, y1, timeline, color, depth, linename) {
timeline.createEmptyMovieClip("line0", depth);
// etc.
And then, to neaten things up, you can use your linename parameter right
in that same inner first line, rather than updating the _name property at
the end. Watch for the difference:
function drawline(x0, y0, x1, y1, timeline, color, depth, linename) {
timeline.createEmptyMovieClip(linename, depth);
timeline[linename].lineStyle(5, color, 100);
timeline[linename].moveTo(x0, y0);
timeline[linename].lineTo(x1, y1);
}
See that? That lets you avoid the extra step. The only reason the
array access operator ([]) is in there is because your linename parameter is
a string. To make it even easier, you can take advantage of the fact that
the createEmptyMovieClip() method returns a value -- which happens to be a
movie clip reference.
function drawline(x0, y0, x1, y1, timeline, color, depth, linename) {
var mc = timeline.createEmptyMovieClip(linename, depth);
mc.lineStyle(5, color, 100);
mc.moveTo(x0, y0);
mc.lineTo(x1, y1);
}
See how that works? If you wanted to, you could even return this same
value with your own function.
function drawline(x0, y0, x1, y1, timeline, color, depth, linename) {
var mc = timeline.createEmptyMovieClip(linename, depth);
mc.lineStyle(5, color, 100);
mc.moveTo(x0, y0);
mc.lineTo(x1, y1);
return mc;
}
That way, you could either use the passed-in linename parameter as your
later reference or the return value of your function:
drawline(32, 54, 65, 128, this, 0xFF0000, 0, "line0");
line0._x = 500; // moves the line to 500px
OR ...
var mc = drawline(32, 54, 65, 128, this, 0xFF0000, 0, "line0");
mc._x = 500; // moves the line to 500px
Tons of options. Just go with the one that makes most sense to you, and
ignore whatever seems to complicated. :)
Post by GuntoIs there some way to change a movieclip's center using
actionscript? Or maybe, when I create a emptyMovieClip,
adjust the center point?
You can see all the movie clip functionality available to you by
checking out the MovieClip class. Unfortunately, there isn't a centerPoint
property, or a registrationPoint property, so you'll have to fake it. But
... if you're drawing these lines yourself, you can determine where the
"center point" is by making sure to draw your two points so that they
overlap the 0,0 point of the movie clip they're in.
David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."