00:00
00:00

As: Following/shooting At Mouse

31,627 Views | 197 Replies
New Topic Respond to this Topic

As: Following/shooting At Mouse 2005-09-06 16:44:55


AS: Main

AS: Following/Shooting at Mouse

I'm not really in the mood for explaining the code, but this will hopefully be a good reference for the numerous requests for Actionscript to make something rotate to mouse and/or shoot at the mouse position.
SAMPLE

ROTATING TO MOUSE

For this part, you'll need one MC: your turret. Chuck it Stage centre, and give it the Instance Name gun
Add this code:

onClipEvent (mouseMove) {
Xd =_root._xmouse-_x; //Get _x distance from gun to mouse
Yd =_root._ymouse-_y; //Get _y distance from gun to mouse
radAngle = Math.atan2(Yd, Xd); //Use atan2 to calculate the angle from gun to mouse
_rotation = int((radAngle*360 / (2*Math.PI))+90); //Use PI to calculate and set gun rotation
updateAfterEvent();
}

CTRL+ENTER to test. Gun should now track the mouse.

MAKING BULLETS

For this, you'll need one more symbol, your bullet. Draw a rectangle pointing upwards, and convert to MC.
Since you'll probably want more than one bullet on-screen at a time, we'll use it as a base and make copies using duplicatedMovieClip(). Don't be scared.

Place an instance of your bullet on Stage, give it the Instance Name bullet, and add these actions to it:

onClipEvent(load){
spd=20; //bullet speed
_x=_root.gun._x; //Move to gun _x
_y=_root.gun._y; //Move to gun _y
_rotation= _root.gun._rotation; //Point in same direction as gun
}
onClipEvent(enterFrame){
if(_name == "bullet"){
_x = -1000; //Move the original bullet MC offstage
}else{
//Run this movement code on all dupes
if (_rotation>180) {
_y += (spd*Math.cos( Math.PI/180*_rotation));
_x -= (spd*Math.sin( Math.PI/180*_rotation));
} else {
_y -= (spd*Math.cos (Math.PI/180*_rotation));
_x += (spd*Math.sin( Math.PI/180*_rotation));
}
//if(hitTest(_root.enemy)){ blahh; } Not going to address this here, check AS: Main for hitTest threads.
}
if(_x>Stage.width || _x<0 || _y<0 || _y>Stage.height){
//If off-stage, delete the dupe to save CPU
this.removeMovieClip();
}

}

FIRING AT MOUSE

Now all we need is the 'trigger' code that will produce copies of the bullet. Once created, the dupes will run their code which will send them off in the direction of the mouse.
Add this code to the first frame, main timeline:

var bc=1000; //bulletcount
_root.onMouseDown=function(){
bc++;
if(bc>1100){ bc=1000; } //Reset bulletcount
duplicateMovieClip("bullet", "b"+bc, bc); //Create dupe bullet
}

That's it.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 16:45:29


Part 2...???

Just an alternative method. You'll need the same MCs, gun and bullet, (place the bullet off-Stage), but this time we'll do everything from the main timeline. Just paste this to the first frame:

bc=1000; //bulletcount
onMouseDown=function(){
bc++;
if(bc>1100){bc=1000;}
duplicateMovieClip("bullet", "b"+bc, bc);
with(_root["b"+bc]){
spd = 20;
_x=gun._x;
_y=gun._y;
_rotation = _root.gun._rotation;
}
_root["b"+bc].onEnterFrame=function(){
with(this){
if (_rotation>180) {
_y += (spd*Math.cos( Math.PI/180*_rotation));
_x -= (spd*Math.sin( Math.PI/180*_rotation));
} else {
_y -= (spd*Math.cos( Math.PI/180*_rotation));
_x += (spd*Math.sin( Math.PI/180*_rotation));
}
if(_x>Stage.width || _x<0 || _y<0 || _y>Stage.height){
this.removeMovieClip();
}
}
}
}

onMouseMove=function(){
with(gun){
this.Xd =_root._xmouse-_x;
this.Yd =_root._ymouse-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int((radAngle*360/ (2*Math.PI))+90);
}
}

If you spend a little time comparing these two methods, you might realise why functions are an excellent way to keep your code organised.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 16:46:07


wow. that was useful.

denvish = <3

Response to As: Following/shooting At Mouse 2005-09-06 16:46:18


Why are you wasting your time on this stupid crap. You should be working on our collab >:(

Just kidding. Nice job. I've seen a lot of questions based on this method, so this should come in handy.

Response to As: Following/shooting At Mouse 2005-09-06 16:49:14


Great, I needed this code for a game i'm planning!!!


If life lets you down, remember you were the fastest sperm to the egg

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 16:53:15


At 9/6/05 04:49 PM, PsychoSunburst wrote: Great, I needed this code for a game i'm planning!!!

I hope you're going to spend some time actually understanding the code, rather than just copy/pasting it...


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 16:54:46


Click me plz

Good tutorial =D


Sup, bitches :)

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 17:03:57


What does int() do ? I've never used it.

Response to As: Following/shooting At Mouse 2005-09-06 17:05:21


At 9/6/05 05:03 PM, T-H wrote: What does int() do ? I've never used it.

Converts up or down to nearest integer.
There's a Math. function that does the same thing, I think, but I prefer int().... less to type, cos I'm a lazy bastard.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 17:07:07


At 9/6/05 05:05 PM, Denvish wrote: Converts up or down to nearest integer.
There's a Math. function that does the same thing, I think, but I prefer int().... less to type, cos I'm a lazy bastard.

Same as Math.round then? kthx

Response to As: Following/shooting At Mouse 2005-09-06 17:19:31


At 9/6/05 04:53 PM, Denvish wrote: I hope you're going to spend some time actually understanding the code, rather than just copy/pasting it...

yeah, I've saved it for later to read I don't like cheating like that


If life lets you down, remember you were the fastest sperm to the egg

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 17:24:30


At 9/6/05 04:44 PM, Denvish wrote: _rotation = int((radAngle*360 / (2*Math.PI))+90); //Use PI to calculate and set gun

May I also ask why use use 360 to convert radians to degrees? Is it just preference?
I use

deg = angle*(180/Math.Pi);

Response to As: Following/shooting At Mouse 2005-09-06 17:28:14


At 9/6/05 05:05 PM, Denvish wrote:
At 9/6/05 05:03 PM, T-H wrote: What does int() do ? I've never used it.
Converts up or down to nearest integer.
There's a Math. function that does the same thing, I think, but I prefer int().... less to type, cos I'm a lazy bastard.

Int() only rounds down. Int( 5.9 ) would come out as 5, for example. If you want to round to the nearest integer, up or down, you should use the Math.round() function.

Response to As: Following/shooting At Mouse 2005-09-06 17:30:52


At 9/6/05 05:24 PM, T-H wrote:
At 9/6/05 04:44 PM, Denvish wrote: _rotation = int((radAngle*360 / (2*Math.PI))+90); //Use PI to calculate and set gun
May I also ask why use use 360 to convert radians to degrees? Is it just preference?
I use

deg = angle*(180/Math.Pi);

Good question. I don't know.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 17:31:56


At 9/6/05 05:28 PM, Begoner wrote: Int() only rounds down.

Same as Math.floor then? seems kinda pointless to have 2 commands that do the same thing, the must be a subtle difference like between random(#) and Math.random()

Response to As: Following/shooting At Mouse 2005-09-06 17:44:23


i love you.

Response to As: Following/shooting At Mouse 2005-09-06 17:46:29


At 9/6/05 05:44 PM, Shomer wrote: i love you.

Well I better let you know about my AIDS before you take this too far.

Response to As: Following/shooting At Mouse 2005-09-06 19:19:06


Well, there is one very subtle difference between int() and Math.floor(), and that's when dealing with negatives. All int() does is take away every digit to the right of the '.', so 5.123 become 5 and also -5.123 becomes 5. Math.floor(), however, returns the number that is closest to, and less than or equal to, the number you are rounding. So:

int( 1.9 ) = 1
Math.floor( 1.9 ) = 1

But:

int( -1.9 ) = -1
Math.floor( -1.9 ) = -2

Response to As: Following/shooting At Mouse 2005-09-06 19:22:06


At 9/6/05 07:19 PM, Begoner wrote: Well, there is one very subtle difference between int() and Math.floor(), and that's when dealing with negatives.

Well. I'm still learning something new every day ;)
Thanks for the info. Dutifully stored in my brain, it'll probably fall out of my ear in a couple of weeks.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-06 20:51:31


Thanks, i had already posted a game at

http://img377.images..ssinteractive8ee.swf

CLick the gun to shoot.

Response to As: Following/shooting At Mouse 2005-09-06 20:55:55


Yeah everyone learns trig (maybe not the radian conversion because calculators automatically do that) but still everyone who has done year 9 at school knows this stuff they just dont know its practical uses (hence why most people didnt listen in class) (guilty)


- Matt, Rustyarcade.com

Response to As: Following/shooting At Mouse 2005-09-07 00:21:16


what if we wanted the turret to aim at a spesific MC??
onClipEvent (mouseMove) {
Xd =_root._xmouse-_x; //Get _x distance from gun to mouse
Yd =_root._ymouse-_y; //Get _y distance from gun to mouse
radAngle = Math.atan2(Yd, Xd); //Use atan2 to calculate the angle from gun to mouse
_rotation = int((radAngle*360 / (2*Math.PI))+90); //Use PI to calculate and set gun rotation
updateAfterEvent();
} what do we change? and what duz what

Response to As: Following/shooting At Mouse 2005-09-07 00:49:45


At 9/6/05 08:51 PM, CUCKA_DUDE wrote: CLick the gun to shoot.

It is damn near impossible to click the little bit of trigget that is the gun without realigning the who gun itself which causes the trigger to move and be unable to fire. The times that I'm quick enough to get to the trigger the alignment has moved and I'm not shooting at anything remotly close to what I was aiming for. If you centered your animation on the trigger and made it only rotate if the distance between the mouse and gun were bigger than a small number that would help. You also need to go into the gun's mc and rotate it until the line of sight of the gun intersets the mouse because that makes it hard to aim too.

At 9/7/05 12:21 AM, NuiStyles wrote: what if we wanted the turret to aim at a spesific MC??
onClipEvent (enterFrame) {
Xd = mc._x-_x;
Yd = mc._y-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int(radAngle*180 / Math.PI+90);
}

^Edited. Give the movieclip you want to aim at the instance name: mc Or change the two places in the code that say mc to whatever your instance name is.

Response to As: Following/shooting At Mouse 2005-09-07 01:13:30


i tryed your way and i tryed this way
onClipEvent (load) {
Xd = _root.hero._x-_x;
Yd = _root.hero._y-_y;
}
onClipEvent (enterFrame) {
radAngle = Math.atan2(Yd, Xd);
_rotation = int(radAngle*180 / Math.PI+90);
}
still dont work

Response to As: Following/shooting At Mouse 2005-09-07 02:54:58


Hmm, when i tried it worked for me but then didnt cuz i fiddled :( but yeah, dont have 2 "On frame events" just delete one, and shit like that. just go over it editing the obvious, you never know...

Response to As: Following/shooting At Mouse 2005-09-13 01:46:37


so I have been messing around with this, its a really nice code, but I can't seem to get my hittest to work with it, is there something special that I am missing to make hittest work with this code?

Response to As: Following/shooting At Mouse 2005-09-13 16:31:43


At 9/6/05 04:44 PM, Denvish wrote: Xd =_root._xmouse-_x;
Yd =_root._ymouse-_y;
radAngle = Math.atan2(Yd, Xd);
_rotation = int((radAngle*360 / (2*Math.PI))+90);
updateAfterEvent();
}

Goddamn I need to learn this stuff!

*Printed*


BBS Signature

Response to As: Following/shooting At Mouse 2005-09-13 16:38:19


At 9/7/05 02:54 AM, Acerbic wrote: Hmm, when i tried it worked for me but then didnt cuz i fiddled :( but yeah, dont have 2 "On frame events" just delete one, and shit like that. just go over it editing the obvious, you never know...

If you deleted one of the events, that's probably why it didn't work. Neither I nor anyone else in this thread has posted code containing two "on frame events"; my code has an onLoad event, which runs on the MC's initial load, and an onEnterFrame event, which runs on every frame.


- - Flash - Music - Images - -

BBS Signature

Response to As: Following/shooting At Mouse 2005-09-13 17:51:35


So may I ask why my hittest isn't working? I know the hittest works, but when I shoot my "bullet" the hittest doesnt work. Am I missing a step?

Response to As: Following/shooting At Mouse 2005-09-13 17:53:55


I was looking through the flash 8 help and saw that int() was supposedly deprecated after Flash 5, it recomends you use Math.round instead.

Just thought I'd let you all know :P