To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Display Modes
Old 09-24-2001, 07:52 AM   #1
lovestreet
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...
lovestreet is offline   Reply With Quote
Old 09-24-2001, 08:39 AM   #2
Stickman
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);
There might well be a simpler way to do this, but I'm relying on my feeble mathematical abilities...
Stickman is offline   Reply With Quote
Old 09-24-2001, 02:03 PM   #3
thedaydreamer
Junior Member
 
Join Date: Sep 2001
Posts: 4
Angry 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.
thedaydreamer is offline   Reply With Quote
Old 09-25-2001, 09:30 AM   #4
lovestreet
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.
lovestreet is offline   Reply With Quote
Old 09-25-2001, 11:00 AM   #5
RazoRmedia
Moderator
 
RazoRmedia's Avatar
 
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");
    }
}
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
RazoRmedia is offline   Reply With Quote
Old 09-25-2001, 01:07 PM   #6
thedaydreamer
Junior Member
 
Join Date: Sep 2001
Posts: 4
Smile

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.
thedaydreamer is offline   Reply With Quote
Old 09-25-2001, 04:24 PM   #7
Stickman
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.
Stickman is offline   Reply With Quote
Old 09-25-2001, 06:08 PM   #8
chris47
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.

chris47 is offline   Reply With Quote
Old 09-26-2001, 03:56 AM   #9
RazoRmedia
Moderator
 
RazoRmedia's Avatar
 
Join Date: Oct 2000
Location: on the door
Posts: 3,016
Quote:
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! )
RazoRmedia is offline   Reply With Quote
Old 09-26-2001, 04:39 AM   #10
Stickman
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;
}
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.
Stickman is offline   Reply With Quote
Old 09-26-2001, 05:12 AM   #11
RazoRmedia
Moderator
 
RazoRmedia's Avatar
 
Join Date: Oct 2000
Location: on the door
Posts: 3,016
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
RazoRmedia is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:24 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.