A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Rotation Problem

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    3

    Rotation Problem

    Alright, sooo I created a 'shotgun'. Its positioned at the bottom middle of the screen. I want it to rotate so that it follows the mouse.
    Code:
    onClipEvent (enterFrame) {
    	var dy = _y-(_root._ymouse*-1);
    	//Adjacent Triangle distance
    	var dx;
    	//Declares opposite distance
    	var angle;
    	//angle from Adj to hyp.
    	if (_root._xmouse<_x) {
    		dx = _x-_root._xmouse;
    		angle = tan(dx/dy);
    		_rotation = angle+90;
    	} else {
    		dx = _root._xmouse-_x;
    		angle = tan(dx/dy);
    		_rotation = 90-angle;
    	}
    }
    What did I do wrong?

  2. #2
    Senior Member Genesis F5's Avatar
    Join Date
    Jan 2002
    Location
    Unallocated memory
    Posts
    1,845
    Hi, Tumms. Welcome to the forums.

    Try this code instead:

    Code:
    onClipEvent (enterFrame) {
    	mox = _root._xmouse;
    	moy = _root._ymouse;
    	_rotation = Math.atan2(moy - _y,mox - _x) * 180 / Math.PI + 90
    }
    Unless you need to use the mouse position coordinates again, just take the variables out and put _root._xmouse and _root._ymouse directly into the equation. I just did it that way for readability.

  3. #3
    Junior Member
    Join Date
    Nov 2006
    Posts
    3
    Works perfectly, thanks! Whats the difference between Math.tan, Math.atan, and Math.atan2?

    One more thing, I'm trying to have bullet holes, but it only keeps one bullet hole when I click. When I click it keeps the black dot but when I click somewhere else the black dot moves, it doesnt duplicate. So..what'd I do wrong with this code? (Thankyou for all your help)

    Code:
    onClipEvent (mouseDown) {
    	_root.bull._x = _root._xmouse;
    	_root.bull._y = _root._ymouse;
    	var amount = 10;
    	var i;
    	while (amount>0) {
    		duplicateMovieClip(_root.bull, "mc"+i, i);
    		amount--;
    		i++;
    	}
    }
    Last edited by tumms; 11-23-2006 at 05:11 PM.

  4. #4
    Flash / Php game designer
    Join Date
    Mar 2005
    Location
    UK
    Posts
    253
    The depth (i) is being reset each time you click so its overwriting the other.

    P.S. Angle code doesn't work for me in Flash 8.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center