Skip to main content
November 3, 2009
Answered

Help with creating movie at cursor position

  • November 3, 2009
  • 1 reply
  • 985 views

Hello all,

I have a project where you click on a movieclip and it creates a new movie on the stage and I do a startdrag on that movie.  Rather I would like to, just to test if I could get it to work I figured out the x and y position roughly on one of the movies and was able to get  the startdrag to work(YaY!!), however I would like the created movieclip if possible to be created centered(center x and y position of the created movie) on the cursor point.

I really have no clue where to begin. I am still fairly green to actionscript coding and reading the help manual is almost a foreign language to me. Though I am getting better daily.

Thanks for any help or suggestions you offer.

Chris

This topic has been closed for replies.
Correct answer Ned Murphy

Ok the only way I can see doing this is moving the content within the movie so that that contents(graphic symbol) center point is at the movies 0, 0 registration point.

Yay! That worked with setting the startDrag(true). However now I have an issue and I am sure it is my lack of experience that the new object is created in the top left corner of the stage and when you start to drag it snaps to the center of the cursor. Is there a way to have it created right on the mouse position before the startDrag?

The only thing popping into my head would be to have the _metalBaluster.x and _metalBaluster.y = -something or other so it is created off the stage then as you drag it looks like it created.

Thanks again for your time.


Set the object x and y properties to equal mouseX and mouseY, respectively.

1 reply

Ned Murphy
Legend
November 4, 2009

What code are you using to create this movieclip?

November 4, 2009

right now it is a skeleton as I build the functionality.

package

{

import flash.display.MovieClip;

import flash.events.MouseEvent;

import metalBalusters;

public class DragDrop extends MovieClip

{

private var _metalBaluster:metalBalusters;

public function DragDrop()

{

this.buttonMode = true;

this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

}

private function onDown(event:MouseEvent):void

{

_metalBaluster = new metalBalusters();

MovieClip(root).addChild(_metalBaluster);

_metalBaluster.buttonMode = true;

_metalBaluster.x = 575;

_metalBaluster.y = 380;

_metalBaluster.startDrag();

_metalBaluster.addEventListener(MouseEvent.MOUSE_UP, dropMovie);

_metalBaluster.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);

}

private function dropMovie(event:MouseEvent):void

{

_metalBaluster.stopDrag();

}

private function dragMovie(event:MouseEvent):void

{

_metalBaluster.startDrag();

}

}

}

Here is a link to the movie.  Click metal and the first baluster on the left of the slider box.  Click on the right side of the baluster image so that it will be on your mouse.

http://goodielcreative.com/test.html

Ned Murphy
Legend
November 4, 2009

The easiest way to get the object to center on the mouse is to edit the object so that its registration point is centered in the movieclip.  Then you can use the lockCenter argument of the startDrag function (set to true, as in: startDrag(true);) to have it centered when clicked on for dragging.  If the registration point is the upper left corner of the object, and having it there is handy for other purposes, then you could also try assigning the x/y values using the mouseX/Y properties in combination with half the width/height values of the objects...

object.x = mouseX - object.width/2

object.y = mouseY - object.height/2