Click to See Complete Forum and Search --> : Line between mouse and MC

01-13-2003, 11:32 AM
Ive tried this but can seem to get it to work properly and im fed up.

What i want is i have a mc which at this point is stationary, and the mouse pointer which moves around the screen. What i want is for their to be a line drawn between the 2 objects. I want it to rotate according to where the mouse is on the screen and also strech and shrink according to the distance between the 2 objects.

If anyone could maybe provide a fla with some comments in it to help me out i would appretiate it alot.

ThankS in AdvancE


01-14-2003, 03:43 AM
Dont tell me no one knows how to do it i see it all the time.

If you dont understand what i want just ask me to explain it agian :)

ThankS in AdvancE


01-14-2003, 03:50 PM
Sorry, i did this when i saw your post, but never got chance to post it as i'm in the middle of exams :mad:

It's not the most efficient/best way of doing it, but it should give you an idea of where to go... no comment tags sorry, but it's pretty self explanatory.

By the way, i did this in MX using the dynamic drawing stuff. Hope you dont want it in flash 5. Possible, sure, but a bit more complicated.

01-16-2003, 04:03 AM
Thanks for that man i really aprettiate it. But i cant download the file. The MX file will just fine thanks agian for doing that :)

01-16-2003, 05:22 AM
I tried it agian man and it worked so thanks heaps its exactly what i wanted. Havent looked at the code yet but be prepared to answer some questions when i have a look :)

01-16-2003, 05:30 AM
LoL didnt realise how easy that is :).

One more quick question. Dunno if you can help me on this. But ill ask ne way. What i want is that onmouse down move a Ball movie clip from the original movie clip in the direction of the line :)

01-16-2003, 11:23 AM
I've done a fairly siply example of what (I think) you're looking for. Copy the below code into the original file, not changed anything to it, only added.

_root.onMouseDown = function()
var speed = 5;

// attach & position ball to mc...
var a = _root.attachMovie("ball", "ball", 2);
a._x = _root.mc._x;
a._y = _root.mc._y;

// which direction it goes...
a.x = (_root._xmouse - _root.mc._x)/speed;
a.y = (_root._ymouse - _root.mc._y)/speed;

a.onEnterFrame = function()
// move it...
this._x += this.x;
this._y += this.y;

// remove if off screen...
if (this._x > Stage.width || this._x < 0 || this._y < 0 || this._y > Stage.height) {

(Hope this comes out nce with the new colour code tags :D)

All you have to do is make an mc with a linkage name of "ball".

There can be only one ball on the screenat a time with this, clicking again when there's already a ball on the screen will replace it with a new one. Also, the way i've done it maens that the ball will go faster the longer the line, as to indicate the strength, if you like, of the force. IT also doesn't stop to where the mouse was, just keeps going to edge of screen.

P.S. Commented just fou you :p

01-16-2003, 09:41 PM
That is exactly what i want. I really appretiate all this.

Question:- Is it better to make all my actionscript into functions ?

I normally only use functions when i need to do multiple things at once. So i dont need to re-write the same lot of code.

01-16-2003, 09:52 PM
Is their a way to limit the distance the line can stretch ?

Sorry to be a hastle :)

01-17-2003, 01:56 PM
No hassle! I like a good problem. :confused: -> :mad: -> :p -> :D


well, of course functions are best for multiple use, that's great that you do that. But another thing they're good for is when you want a block of code to run. You don't want the "end screen" codes to run when the movie first plays, so you can put them in a function, and call them when they are needed. Of course, if you've got them all on different frames, then you probably won't need to do that. But i always try to code on a single frame if i can. Mostly for games, so no-one can cheat by clicking play, and going to next frame :D he he.

restricted line length

well, this was a bit tougher than i first though. So much so, i had to sleep on it. Turned out quite good though i think. There's a fair bit of code changes, so i'll just attach the file again. It's fairly easy to understand if you know a bit of basic of trig.

if (line too long) {
shorten it to the max length;
} else {
don't bother - it's fine!;

stuff like that. Trig comes in when you want to shorten it to max length. Change length with variable linelength. That's basically it.

What is it you're using it for anyway? Would like to know. Good luck!

01-17-2003, 08:57 PM
Thanks for that its exactly what i wanted, you have been a great help.

One more question and i think its my last :)

Would i use the same techniques to restrict the line to a 90 degree movement. So the line can only move from 270 - 360 degree angle

ThankS in AdvancE


01-17-2003, 10:21 PM
Lol so there are only 4 spots the line can be in?

Cause you can just do that with a button instead of a script =)
just oppinion

01-17-2003, 10:34 PM
i think hooligan maybe meant restrict it in the 270 -> 360 quadrant. (9 o'clock to 12 o'clcok only) ???

You would only need to use the "endx = mcx-linelength*Math.cos(angle);" lines (the one's with negative signs) if you wanted to do that, because the "+" ones are for the right hand side of the circle. But yeah, same theory, just another if statement should do it.

Have a go, see what you can do. I'll not post code, you should be able to do this :) Practicing is the best way to learn. Will still help on anything more if you can't, of course.

01-18-2003, 12:23 AM
LOL stupid me. Didnt look at the code properly :) woops

Thanks Jonmack you have been a great help.

Also thanks for your input Nionicle

01-18-2003, 12:31 AM

I wish i could have helped more but i dont have mx =(

01-20-2003, 03:26 AM
Nah thats no problem, least you tried to help :) Keep it up.