I have this example of hw to make a movie clip follow the mouse by using the
current position of the mouse (_Mousex and _mousey)...but now i wnt the movie clip to
go the opposite direction of the mouse..4 e.g if the mouse is @ Y=5 and X=2 then the movie clip 2
b @ Y=-5 and X=-2...is there a way of doing this,thanx

Dani AI

Generated

— the clip is jumping to the top-left because of coordinate‑space feedback. When you read _xmouse/_ymouse from inside the clip, those values are already relative to the clip. As the clip moves, the mouse coordinates change too, and the math can lock the clip to a corner. was right to flag the AS version, but the real fix is to use stage/global mouse coords and a stable "home" (center) to mirror around.

A cleaner approach:

  • Put the movie clip at the stage center in the authoring panel (or store its initial position on load).
  • Read the mouse position from the root (stage) so coordinates are stable.
  • Mirror the mouse around the stored home point with mirror = 2*home - mouse.
  • Smooth the motion with interpolation (_x += (target - _x)/speed) instead of overwriting every frame.

Example (AS2, different from the code already posted):

onClipEvent(load) {
    speed = 10;
    homeX = this._x; // set by placing clip at stage center
    homeY = this._y;
}

onClipEvent(enterFrame) {
    targetX = 2*homeX - _root._xmouse;
    targetY = 2*homeY - _root._ymouse;
    this._x += (targetX - this._x) / speed;
    this._y += (targetY - this._y) / speed;
}

Notes: s _x+=200/_y+=100 offset works as a quick hack because it shifts the origin, but it is brittle (stage size, registration point or resizing breaks it). If using AS3, use stage.mouseX/mouseY, stage.stageWidth/Height and the event model (no onClipEvent). Adjust speed to tune responsiveness.

Recommended Answers

All 6 Replies

I'm not sure I got your question, but I guess it's simple...
0 - mouseX
0 - mouseY
By the way, you are using an old version of AS

Thanx bt i hv tried it n it dsnt work...pls try it n c if it works coz i might b making a small mistake.

Show me the code...

i have this code

onClipEvent (load)
{
//current position of the movie clip
this._x = 0;
this._y = 0;
//speed
var speed = 20;
}
onClipEvent (enterFrame)
{
//position of the movie clip =position of the mouse -prev position of the movie clip at that speed
_x =- (_xmouse - _x)/speed;
_y =- (_ymouse - _y)/speed;
}

it seems to work but the movie clip goes 2 the top left corner n stucks there,i want it 2 b in the middle..pls help ,thanx 4 replying

Member Avatar for Member #334542

The below code will work

onClipEvent (load)
{
	var speed = 10;
} 

onClipEvent (enterFrame)
{
//position of the movie clip =position of the mouse -prev position of the movie clip at that speed
_x = -(_xmouse - this._x)/speed;
_y = -(_ymouse - this._y)/speed;
_x+=200;
_y+=100;
}
Member Avatar for Member #334542

The below code will work

onClipEvent (load)
{
	var speed = 10;
} 

onClipEvent (enterFrame)
{
//position of the movie clip =position of the mouse -prev position of the movie clip at that speed
_x = -(_xmouse - this._x)/speed;
_y = -(_ymouse - this._y)/speed;
_x+=200;
_y+=100;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.