Be a Supporter!

AS2 Rotation within another mc

  • 927 Views
  • 23 Replies
New Topic Respond to this Topic
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
AS2 Rotation within another mc 2010-12-02 09:05:59 Reply

hey yall, wonderin if you could help me, ive got my ARM mc within my GUY mc so that the guns the guy can shoot stay on him and dont lagg slightly if there attached otherwise. Now my rotation code wont work, can anyone fix this for me?

guy.arm.onEnterFrame = function() {
xxd = _root.guy.arm._x-_root._xmouse;
yyd = _root.guy.arm._y-_root._ymouse;
radAngle = Math.atan2(yyd, xxd);
guy.arm._rotation = radAngle*180/Math.PI+90;
};

ive tried putting _parent._root._xmouse etc but that just fails it

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-02 10:05:34 Reply

ive heard local to global could help me but im having trouble on where to put it anyone?

CyberXR
CyberXR
  • Member since: Aug. 22, 2008
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS2 Rotation within another mc 2010-12-02 10:17:48 Reply

Had the same problem with my game actually. I gave up and did put the arm outside the player MC. Worked perfect for me without any lag


Check out the coolest Donkey Kong Game on NG!
http://www.newgrounds.com/portal/vi ew/459064
100 posts @ Posted at: 9/15/08 07:36 AM http://www.newgrounds.com/bbs/topic /969232

mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to AS2 Rotation within another mc 2010-12-02 10:32:12 Reply

The problem is that you're comparing arm._x (which is relative to guy's coordinates) to _root._xmouse (which is relative to _root's coordinates). You can apply _xmouse to any clip to get the mouse position relative to that clip. Give this a shot:

guy.arm.onEnterFrame = function() {
xxd = _root.guy.arm._x-_root.guy._xmouse;
yyd = _root.guy.arm._y-_root.guy._ymouse;
radAngle = Math.atan2(yyd, xxd);
guy.arm._rotation = radAngle*180/Math.PI+90;
};

Hope this helps.

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-02 11:04:43 Reply

That worked wonders, thank you Mr Mike!

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS2 Rotation within another mc 2010-12-02 11:55:01 Reply

At 12/2/10 10:32 AM, mike wrote: guy.arm.onEnterFrame = function() {
xxd = _root.guy.arm._x-_root.guy._xmouse;
yyd = _root.guy.arm._y-_root.guy._ymouse;
radAngle = Math.atan2(yyd, xxd);
guy.arm._rotation = radAngle*180/Math.PI+90;
};

Well, I never used as2 that much but I always saw this code when it came to rotation.
I once tried it and got quite confused because I didn't know of the relative mouse position thingy.

I never had the urgency to find the proper solution, because I don't use as2.
What's different now is that I don't like to see something that doesn't make any sense.
And yes, this code kinda sucks. (even with things like parent, it would still be horrible)

So I got a flash 8 trial to travel back in time and find myself in the confusing world of AS2.
However it is pretty easily to wrap your had around the whole system.
Any disadvantage is an advantage.

If you are in a relative world, thinking the absolute way is quite hard.
(it's similar to the difference between polar and Cartesian coordinate system:
if the problem is "round" use polar coords for sanity's sake)

Putting that into practice means if the problem is relative, think relative:

_rotation -= (Math.atan2(_xmouse,  _ymouse) * 180 / Math.PI -90);

No dependencies, no _root, no bullshit. (yeah, that's pretty much a beer ad quote)

That's it. I'm leaving the past now, back to AS3...

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-05 22:41:46 Reply

I used the code that Mike gave me and it was fine, but im trying to go a little deeper and have the rotating object within two different movieclips, I've tried it below but it's not working, could anyone help me out on this one?

guy.stand.arm.onEnterFrame = function() {
xxd = _root.guy.stand.arm._x-_root.guy._xmouse ;
yyd = _root.guy.stand.arm._y-_root.guy._ymouse ;
radAngle = Math.atan2(yyd, xxd);
guy.stand.arm._rotation = radAngle*180/Math.PI+90;
};

Thats what I've tried but goddam it dont work! lol

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-06 08:09:24 Reply

ich bin

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-12 00:14:52 Reply

im still having this problem culd any1 help??

Jaface
Jaface
  • Member since: Aug. 26, 2010
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS2 Rotation within another mc 2010-12-12 00:25:44 Reply

At 12/5/10 10:41 PM, SmallPete wrote:

guy.stand.arm.onEnterFrame = function() {

xxd = _root.guy.stand.arm._x-_root.guy._xmouse ;
yyd = _root.guy.stand.arm._y-_root.guy._ymouse ;
radAngle = Math.atan2(yyd, xxd);
guy.stand.arm._rotation = radAngle*180/Math.PI+90;
};

You calculate xxd and yyd with _root.guy.stand.arm, but set rotation to guy.stand.arm.

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-12 00:42:10 Reply

At 12/5/10 10:41 PM, SmallPete wrote: I used the code that Mike gave me and it was fine, but im trying to go a little deeper and have the rotating object within two different movieclips, I've tried it below but it's not working, could anyone help me out on this one?

guy.stand.arm.onEnterFrame = function() {
xxd = _root.guy.stand.arm._x-_root.guy._xmouse ;
yyd = _root.guy.stand.arm._y-_root.guy._ymouse ;
radAngle = Math.atan2(yyd, xxd);
guy.stand.arm._rotation = radAngle*180/Math.PI+90;
};

Thats what I've tried but goddam it dont work! lol

this was explained to you already, your calling the relative mouse position of the guy, but rotating guy.stand.arm thus _root.guy.stand.arm._xmouse etc...


BBS Signature
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-13 21:10:29 Reply

yes i understand that, but its not working, hense the reason I reposted, its not like im trying to be a dick or anything but I seriously cant get around it...

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-14 12:45:05 Reply

guy.stand.arm.onEnterFrame = function() {
guy.stand.arm._rotation = Math.atan2(_root.guy.stand.arm._y-_root.guy.stand._ymouse, _root.guy.stand.arm._x-_root.guy.stand._xmouse)*180/Math.PI+90;
};

doesnt work?


BBS Signature
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-14 21:24:03 Reply

well it rotates the gun, but not to the mouse, infact it goes in the opposite direction all the time. I've tried to fiddle about with the alignment of the object, i.e facing it up, down, left, right, etc but whatever I do it always aims AWAY from the mouse not towards it...

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-14 23:22:11 Reply

guy.stand.arm.onEnterFrame = function() {
guy.stand.arm._rotation = Math.atan2(_root.guy.stand.arm._y-_root.guy.stand._ymouse, _root.guy.stand.arm._x-_root.guy.stand._xmouse)*180/Math.PI+90;
};

time to learn a piece of the code :D

did you learn tangent/atan (reversed tangent), basically its a line drawn on the outside of a circle that when comes to a point with the radius forms a 90 angle, thus creating a triangle, theres an opposite side this would be the y axis of your right trangle; an adjacent side your x axis; and your hypotenus which would be the longest side, but you want the angle. tangent works by creating a 90 degree angle, and if you have 2 side lengths and an angle of a right triangle you can find all details about it, tangent just deals with opposite/adjacent, thats why its used here. now the *180/Math.PI is just converting the radians that tangent gives you (which is a ratio) to degrees. +90 tells it that it needs to set the triangle at a 90 degree offset due to graphics alignment..


BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-14 23:30:00 Reply

also forgot to mention

http://www.newgrounds.com/dump/item/e521 5c005572cf500a96c401572a1940

those little cross hairs on the middle of the stage in each child (movieclip) those are pivot points, not the moveable one you can use for animation but the one where you cant relocate the crosshairs but you can relocate the arm? kinda hard to explain


BBS Signature
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-15 10:46:53 Reply

so what are you saying that it has to be exactly aligned to that point so that it can rotate from the right point?

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-15 11:07:54 Reply

i wont be able to move the arm because of the animation it has and the way it looks, is there anyway around that??

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-15 19:31:05 Reply

no you dont have to move it like that.. just keep n mind those cross hairs is the pivot point of the mc and it will rotate around that point

http://www.newgrounds.com/dump/item/dd42 543d824c6808d5f30aa6a4de08d0

that example uses the same script 3 times (except for +90 -90) but the white dot is the pivot point (the cross hair in flash


BBS Signature
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-16 14:33:21 Reply

ah ok i understand, but because i have the guy.stand.arm (3 movieclips) which pivot point does it rotate by? and also what if i dont want it 2 rotate from the pivot point but rotate it from exactly where it is?

for example do i make a movieclip called 'pivotpoint_whatever' at the exact point i want it to rotate from and then use a code to rotate from that? this way in other movieclips i can move the player i.e guy.RUN and not have it always rotating from the pivot point but insted have it moving from an invisible movieclip placed ontop of the boddy...

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-16 14:41:45 Reply

no.... the pivot point is that cross hair.... and where does your arm rotate from?

a) the hand
b) the elbow
c) the shoulder

its not a cause then your arm would be dismembered from the shoulder, and its not the elbow cause of the same reason except his dismembered arms hand could touch his shoulder... so its c the shoulder, the pivot point is the center of the stage in the child... and you rotate from the shoulder.....


BBS Signature
SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-16 15:06:14 Reply

c yes i understand but my arm just does not rotate properly it will never point to the cursor no matter how much i rotate it and muddle about with the alignment...

SmallPete
SmallPete
  • Member since: Aug. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS2 Rotation within another mc 2010-12-16 15:14:50 Reply

ive done it, for future reference what i did was face the arm down and exactly to alignment, but not the ARM but what was INSIDE the arm MC. Thanks for your help. LOVE YOU <3

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS2 Rotation within another mc 2010-12-16 16:23:49 Reply

thats what i was trying to say, but atleast you figured it out


BBS Signature