|
-
A tricky one, I believe.
I have an mc that follows the mouse. The mc has 8 frames, each with a different view of a car. What I´d want the mc to do is go to a different frame, depending on the direction to which the car is moved. The purpose is to simulate a car that always has its nose facing the right direction.
Obviously, what I need is some kind of a mouse angle detection that is in relation to the current _x and _y coordinates of the movie clip. I´ve been exploring flash junkie´s mouse angle tutorial, and it has elements i´ve been looking for, but i´d like to know if there´s an easier way?
Please help a guy in need of advice...
-
I'm assuming your frames are numbered 1 to 8, with the car rotating clockwise at 45 degree increments, starting with it facing directly upwards.
Try this code, in the car's object actions:
Code:
diffx = _x - _root._xmouse;
diffy = _y - _root._ymouse;
a=((Math.atan (diffy/diffx)) * (180/Math.PI)) + 90;
if (diffx >= 0) {
a+=180;
}
frameNum = math.floor(((math.floor(a/22.5) + 1)%16)/2)+1;
this.gotoAndStop(frameNum);
There might well be a simpler way to do this, but I'm relying on my feeble mathematical abilities...
-
I've done this
strangely enough I already have a rotating F1 car for a game I made. I never finished it properly so its a bit jerky but I know why (my dodgy maths). I used .rotate instead of an eight frame system. i would be happy to e-mail you my file if it will help.
-
Stickman´s code had errors I couldn´t figure out myself, since I´m not that much of an actionscript specialist.
Thanks to daydreamer, but I´m going to use an isometric portrayal, so just rotating one object won´t do...
Seems like I´m way over my Actionscript skill level with this one... well, if I wasn´t I wouldn´t have asked this question in the first place.
Well, if you´ve got ideas, gimme a hand anyhow. Maybe I´ll learn something.
-
Senior Member
label each of the 8 frames as thus
left
right
up
down
right-up
right-down
left-up
left-down
and then on the car movieclip, assign the following actionscript:
Code:
onClipEvent (enterFrame) {
mousex = _root._xmouse;
mousey = _root._ymouse;
currentx = getProperty (this, _x);
currenty = getProperty (this, _y);
if ((mousex > currentx )and(mousey > currenty)) {
gotoAndStop ("down-right");
}
if ((mousex > currentx )and(mousey < currenty)) {
gotoAndStop ("up-right");
}
if ((mousex < currentx )and(mousey > currenty)) {
gotoAndStop ("down-left");
}
if ((mousex < currentx )and(mousey < currenty)) {
gotoAndStop ("up-left");
}
if ((mousex = currentx )and(mousey < currenty)) {
gotoAndStop ("up");
}
if ((mousex = currentx )and(mousey > currenty)) {
gotoAndStop ("down");
}
if ((mousex > currentx )and(mousey = currenty)) {
gotoAndStop ("right");
}
if ((mousex = currentx )and(mousey > currenty)) {
gotoAndStop ("left");
}
}
although, it would look better if you used trig to smoothly rotate the vehicle as stickman said.
this'll do as a substitute if maths and physics scare the living beejesus out of you.
Bruce
-
wow razo thats such a simple idea and it'll work. using trig is a pain in the bum and is really jerky so I recon 8 frames is just as good.
-
Nothing wrong with the script: to prove it, I put together a very simple demo file to demonstrate it in action.
Hope it helps.
-
Razo's method is quite simple but it means that up/down/left/right will only occur when the mouse is exactly in the right place (ie only about 4 degrees out of 360).
To be honest, I think your best bet is to take a deep breath and figure the trig - you will need it if you are going to develop any games with any real complexity.
The Friends of Ed books cover this stuff in quite a lot of detail.
-
Senior Member
Originally posted by chris47
Razo's method is quite simple but it means that up/down/left/right will only occur when the mouse is exactly in the right place (ie only about 4 degrees out of 360).
not if you amend it slightly
Code:
if ((mousex > currentx )and((mousey < currenty+10)and (mousey > currenty-10)) ){
gotoAndStop ("right");
}
(oh, also make sure the up, left, right and down if statements appear first in the as! )
-
I'm curious -- what's the problem with the script I suggested? It works for me, and doesn't suffer from any of the problems of Razor's method.
In fact, I can simplify implementation even further by making it into a function. Just insert this code into the first frame of your main timeline:
Code:
function getFrame (x,y,div) {
var diffx = x - _root._xmouse;
var diffy = y - _root._ymouse;
var divAngle = 360/(div*2);
var a=((Math.atan (diffy/diffx)) * (180/Math.PI)) + 90;
if (diffx >= 0) {
a+=180;
}
var frameNum = math.floor(((math.floor(a/divAngle) + 1)%(div*2))/2)+1;
return frameNum;
}
Now to implement it, all you need to do is put this code into the car movie clip's object actions:
Code:
onClipEvent (enterFrame) {
this.gotoAndStop(_root.getFrame(_x,_y,8));
}
You don't need to worry how the function works, just as long as you send it the correct values. The first two values sent to the function are the x and y coordinates of the car (you don't need to change these), while the last is the number of frames in your animation (so you could even change the number of frames without breaking the script).
I hope this is clear.
-
Senior Member
the code is fine stickman.
i just think that your scaring people with your trig.
If stickmans code looks overwhelming, point your little browsers here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|