|
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Oct 2000
Posts: 109
|
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... |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
|
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);
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Sep 2001
Posts: 4
|
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.
|
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Oct 2000
Posts: 109
|
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. |
|
|
|
|
|
#5 |
|
Moderator
Join Date: Oct 2000
Location: on the door
Posts: 3,016
|
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");
}
}
this'll do as a substitute if maths and physics scare the living beejesus out of you. Bruce |
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Sep 2001
Posts: 4
|
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.
|
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
|
Nothing wrong with the script: to prove it, I put together a very simple demo file to demonstrate it in action.
Hope it helps. |
|
|
|
|
|
#8 |
|
Junior Member
Join Date: Sep 2001
Posts: 26
|
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. |
|
|
|
|
|
#9 | |
|
Moderator
Join Date: Oct 2000
Location: on the door
Posts: 3,016
|
Quote:
Code:
if ((mousex > currentx )and((mousey < currenty+10)and (mousey > currenty-10)) ){
gotoAndStop ("right");
}
|
|
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Jul 2000
Location: Not on the dole any more
Posts: 1,040
|
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;
}
Code:
onClipEvent (enterFrame) {
this.gotoAndStop(_root.getFrame(_x,_y,8));
}
I hope this is clear. |
|
|
|
![]() |
|
||||||
| Thread Tools | |
| Display Modes | |
|
|