Discussion:
Rotate a line!
(too old to reply)
Gunto
2009-03-31 09:54:30 UTC
Permalink
I created a two lines on the root, both are named "line1" but each one is in a
different depth.
I want to call them from another MovieClip, but I can't write
".._root.line1........" because both are named like that.
Is there a way to relate to their depth?
THANKS!
David Stiller
2009-03-31 13:56:38 UTC
Permalink
Gunto,
I have a function that draws the lines, and it returns the
x0,y0,x1,y1,color,depth
What's that function? Can you show the code you used?
I tried to return another variable called "linename" and name
it when I call the func., but then I cant tell it to move,
I can't say : linename.moveTo. . .... ...
This all depends on how your function is written. Show some code, and
NedWebs or I, or someone else, will be able to help you pick through what
you've done.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."
Gunto
2009-03-31 14:24:03 UTC
Permalink
acctually I solved this, thanks!

function drawline(x0,y0,x1,y1,color,depth,linename){
this.createEmptyMovieClip("line0", depth);

line0.lineStyle(5, color, 100);
line0.moveTo(x0, y0);
line0.lineTo(x1, y1);
line0._name=linename;
}

and when I call the function I name it. It works. Thanks!
Now, another issue:
Is there some way to change a movieclip's center using actionscript?
Or maybe, when I create a emptyMovieClip, adjust the center point?
David Stiller
2009-03-31 15:00:34 UTC
Permalink
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 Gunto
Is 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."
Gunto
2009-03-31 17:47:43 UTC
Permalink
I managed to use what you showed me, and I also used your advice about the 0,0
thing.
Now - how do I measure distance between two objects? Without any complicated
equations?
Or - Can I measure a line's length?
THANK YOU
David Stiller
2009-03-31 18:16:31 UTC
Permalink
Gunto,
Post by Gunto
I managed to use what you showed me, and I also used
your advice about the 0,0 thing.
Cool.
Post by Gunto
Now - how do I measure distance between two objects?
Without any complicated equations?
Well ... the complexity of the measurement depends on how the objects
are positioned. If you have a line (or a circle, or any artwork at all)
wrapped in a movie clip and positioned at, say, 50,50 -- and then another
movie clip at 50,100 -- it's pretty easy to measure the distance between
those clips' registration points. They're both at 50px in from the left, so
the _x value doesn't matter. After that, it's easy to subtract 50 from 100
and determine that they're 50px apart, vertically.

I'm guessing that you're actually referring to the start and end points
of each of your lines, though. Even in that case, the principle is the
same. Of course, if your points don't share x or y values, it gets harder
to determine the length of the line segment between them.

Can it be done without "complicated equations"? No. But honestly, the
equation needed isn't all that complicated. :) You'll be using the
Pythagorean theorem (a² + b² = c²;
http://en.wikipedia.org/wiki/Pythagorean_theorem).
Post by Gunto
Or - Can I measure a line's length?
You already have the x and y values for both ends of the line (you're
calling those x0,y0 and x1,y1). So all you have to do is run those through
the theorem. Here's how it works:

a-squared + b-squared = c-squared

That means the value of c (the length you're looking for) is basically
the square root of whatever number you get by adding the squared values of a
and b, which are determined by your points.

To get "a", subtract your two x values to get the difference between
then. To get "b", do the same for your y values, then run the numbers.

Here are your x and y values:

var x0 = 32;
var x1 = 65;

var y0 = 54;
var y1 = 128;

(again, you already have these; I'm just using values you've used in
previous replies) ...

Now you use those values to give you your a and b:

var a = x0 - x1;
var b = y0 - y1;

And finally, you get c (your length) by taking the square root of the
sum of the squares of a and b:

var c = Math.sqrt((a * a) + (b * b));


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."
Gunto
2009-03-31 21:00:14 UTC
Permalink
Hello!
Thank you.
Now I want to refer to one of the properties the function returned,
example: _root.stage.line7.x0
How do I refer to one of these variables?
David Stiller
2009-04-01 14:53:29 UTC
Permalink
Gunto,
Post by Gunto
Now I want to refer to one of the properties the function returned,
example: _root.stage.line7.x0
The function only returns a value if it contains the "return" keyword,
which was in one of my code samples, but not all of them. If you return the
movie clip itself, then you could possibly add variables (actually dynamic
properties) to that movie clip before returning it. Something like this:

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);
mc.x0 = x0;
mc.x1 = x1;
mc.y0 = y0;
mc.y1 = y1;
return mc;
}

Then you could refer to that returned value as I showed before, making
use of its additional new properties:

var tempMC = drawline(32, 54, 65, 128, this, 0xFF0000, 0, "line0");
tempMC._x = 500; // moves the line to 500px
trace(tempMC.x0); // retrieves the clip's custom x0 property

But why not make it even easier on yourself? Add your Pythagorean
theorem to your drawline() function and give your movie clip a length
property:

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);
var a = x0 - x1;
var b = y0 - y1;
mc.length = Math.sqrt((a * a) + (b * b));
return mc;
}

Then ...

var tempMC = drawline(32, 54, 65, 128, this, 0xFF0000, 0, "line0");
trace(tempMC.length); // retrieves the clip's custom length property


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

Gunto
2009-03-31 13:38:18 UTC
Permalink
I have a problem with that,
I have a function that draws the lines, and it returns the variables:
x0,y0,x1,y1,color,depth
and later on it draws the line:

line1.lineStyle(3, color, 75);
line1.moveTo(x0, y0);
line1.lineTo(x1, y1);

I tried to return another variable called "linename" and name it when I call
the func., but then I cant tell it to move,
I can't say : linename.moveTo. . .... ...
What can I do?
NedWebs
2009-03-31 13:04:03 UTC
Permalink
Why don't you name them differently?
Loading...