PDA

View Full Version : How to make object centered on mouse?


Heloed
08-02-2008, 05:50 AM
Hi, I am making a game in CS3 with dragging and dropping of objects, and I was able to find a tutorial that has helped me greatly.

I made a 3D crate, and made it so if you click while over it you can drag it, and if you release mouse button over it it will release it. The problem is that when you click on it the mouse moves to the upper-left corner of the bounds of the object where there isn't any actual object, so if you release mouse button it doesn't do anything and the object stays in drag-mode.

How can I make it so when I click on the object it will stick to my mouse exactly where I clicked, or alternatively will stick at a specific point on the object that I choose?

Edit: Thank you to all who viewed this thread but did not answer a very simple question. I have figured it out on my own. For anybody else who searched for this question and found this thread, here is the answer:

When you open the screen to make the item into a movieclip/button there is a 3x3 grid that has one of the boxes filled in. You can use this to change where the item registers at.
Also you can make it even more specific by (after having created the mc) double-clicking to go into the editor and then you should be able to move the white star thing to wherever, this is the registration point.

DiamondDog
08-02-2008, 10:21 PM
If I've understood correctly, you might want to try something like this.

I've got a symbol in my library linked to the 'Box' class.
I put an instance of that symbol on the stage.

When I first click on that instance, I calculate the difference (the 'offset')
between the symbol's coordinates (at its top left corner) and the position
of the mouse.

Then I keep using those offset values in the 'keepDragging' function

var myBox:Box = new Box();
addChild(myBox);
myBox.x = 100;
myBox.y = 200;

var Xoffset:int;
var Yoffset:int;

myBox.addEventListener(MouseEvent.MOUSE_DOWN,start Dragging);

function startDragging(e:MouseEvent)
{
// grab the offset
Xoffset = mouseX - myBox.x;
Yoffset = mouseY - myBox.y;

e.target.addEventListener(Event.ENTER_FRAME,keepDr agging);
}

function keepDragging(e:Event):void
{
myBox.x = mouseX - Xoffset;
myBox.y = mouseY - Yoffset;
}

There's probably other ways of doing it.

Attached is my .fla file.

DiamondDog
08-02-2008, 10:23 PM
For some reason the .fla file didn't upload.

No wonder, I forgot to put it in a .zip. Try again ...