PDA

View Full Version : Scale


rocskyline
03-21-2011, 12:54 AM
Hello, I'am trying to replicate a Mouse Pad with actionscript. This is what I have so far:

var rect:Rectangle = new Rectangle(0,0,pad.width, pad.height);

pad.mouse.addEventListener(MouseEvent.MOUSE_DOWN, doDrag);

function doDrag(e:MouseEvent):void{
e.target.startDrag(false,rect);
stage.addEventListener(MouseEvent.MOUSE_UP, doNotDrag);
pad.mouse.addEventListener(MouseEvent.MOUSE_MOVE, movingMouse);
e.target.removeEventListener(MouseEvent.MOUSE_DOWN , doDrag);
}

function doNotDrag(e:MouseEvent):void{
pad.mouse.stopDrag();
pad.mouse.addEventListener(MouseEvent.MOUSE_DOWN, doDrag);
pad.mouse.removeEventListener(MouseEvent.MOUSE_MOV E, movingMouse);
stage.removeEventListener(MouseEvent.MOUSE_UP, doNotDrag);

}

function movingMouse(e:MouseEvent):void{
beetle.x= e.target.x*5;
beetle.y= e.target.y*5;
}


This works, however, for every 1 pixel I move on that pad the object moves 5, since my mouse pad mc is 5x smaller than the stage.

Thank you very much for your time and help.

ps: I have attached a cs4 fla file if you want to take a look at it.

Mazoonist
03-21-2011, 05:43 AM
You didn't ask any question. So, what are you looking for? A way to make it work better?

henke37
03-21-2011, 10:39 AM
He needs help redoing the movingMouse function so that it doesn't do the multiplication.

rocskyline
03-22-2011, 06:01 AM
Sorry, if I was unclear. My question is can I drag it by 0.2 pixels so when I multiply it by 5 it will equal to 1 pixel thus resulting in a smooth pixel by pixel animation. Or, maybe use pseudo physics in between the 5 pixels.

Mazoonist
03-22-2011, 06:33 AM
What you really need to do is get the ratio of the mouse's x position compared to the x distance it can travel on the pad. Then take that same ratio and apply it to the beetle and the distance it is allowed travel on the stage. Do the same with the y direction. So instead of multiplying by 5 pixels, you are multiplying by this calculated ratio, which is a decimal number between 0 and 1.

That way, the mouse pad can be any size, and your stage can be any size, and they need not even be proportional to each other, although they still can be, of course, if you want.

I gave your file a makeover along these lines (attached). I changed the registration point in a couple of the symbols, and I took the mouse and placed it in the same coordinate space as the pad, instead of nested inside of it, because it made it easier for me. I also handled the drag and drop a little bit differently. I hope this helps. It's almost the same code I just used making a scrollbar tutorial on my website, so I just applied the same principles to this.

rocskyline
03-22-2011, 07:37 AM
Mazoonist, Thats exactly what I was looking for. You saved me a lot of time and hair pulling. Thank you very much good sir.