Forum Topic: As: Following/shooting At Mouse

(19,420 views • 199 replies)

This topic is 7 pages long. [ 1 | 2 | 3 | 4 | 5 | 6 | 7 ]

<< < > >>
None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 04:44 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 04:45 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

arctichigh

Reply To Post Reply & Quote

Posted at: 9/6/05 04:46 PM

arctichigh EVIL LEVEL 05

Sign-Up: 08/27/05

Posts: 358

wow. that was useful.

denvish = <3


None

Deathcon7

Reply To Post Reply & Quote

Posted at: 9/6/05 04:46 PM

Deathcon7 LIGHT LEVEL 20

Sign-Up: 10/01/03

Posts: 5,142

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.


None

Wretched-Raygun

Reply To Post Reply & Quote

Posted at: 9/6/05 04:49 PM

Wretched-Raygun DARK LEVEL 19

Sign-Up: 02/09/05

Posts: 1,684

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 04:53 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

liaaaam

Reply To Post Reply & Quote

Posted at: 9/6/05 04:54 PM

liaaaam NEUTRAL LEVEL 21

Sign-Up: 12/11/04

Posts: 12,766


None

T-H

Reply To Post Reply & Quote

Posted at: 9/6/05 05:03 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 05:05 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

T-H

Reply To Post Reply & Quote

Posted at: 9/6/05 05:07 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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


None

Wretched-Raygun

Reply To Post Reply & Quote

Posted at: 9/6/05 05:19 PM

Wretched-Raygun DARK LEVEL 19

Sign-Up: 02/09/05

Posts: 1,684

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

None

T-H

Reply To Post Reply & Quote

Posted at: 9/6/05 05:24 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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);


None

Begoner

Reply To Post Reply & Quote

Posted at: 9/6/05 05:28 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,064

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 05:30 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

T-H

Reply To Post Reply & Quote

Posted at: 9/6/05 05:31 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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()


None

Shomer

Reply To Post Reply & Quote

Posted at: 9/6/05 05:44 PM

Shomer EVIL LEVEL 13

Sign-Up: 06/23/05

Posts: 649

i love you.


None

T-H

Reply To Post Reply & Quote

Posted at: 9/6/05 05:46 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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.


None

Begoner

Reply To Post Reply & Quote

Posted at: 9/6/05 07:19 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,064

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 9/6/05 07:22 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

Happy

CUCKA-DUDE

Reply To Post Reply & Quote

Posted at: 9/6/05 08:51 PM

CUCKA-DUDE EVIL LEVEL 11

Sign-Up: 07/08/05

Posts: 297

Thanks, i had already posted a game at

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

CLick the gun to shoot.


None

Rustygames

Reply To Post Reply & Quote

Posted at: 9/6/05 08:55 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,429

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


None

NuiStyles

Reply To Post Reply & Quote

Posted at: 9/7/05 12:21 AM

NuiStyles LIGHT LEVEL 03

Sign-Up: 07/10/04

Posts: 367

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


None

BleeBlap

Reply To Post Reply & Quote

Posted at: 9/7/05 12:49 AM

BleeBlap LIGHT LEVEL 24

Sign-Up: 03/08/05

Posts: 945

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.


None

NuiStyles

Reply To Post Reply & Quote

Posted at: 9/7/05 01:13 AM

NuiStyles LIGHT LEVEL 03

Sign-Up: 07/10/04

Posts: 367

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


None

Acerbic

Reply To Post Reply & Quote

Posted at: 9/7/05 02:54 AM

Acerbic DARK LEVEL 31

Sign-Up: 11/07/04

Posts: 2,099

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...


None

SupaTuna

Reply To Post Reply & Quote

Posted at: 9/13/05 01:46 AM

SupaTuna NEUTRAL LEVEL 19

Sign-Up: 06/18/04

Posts: 629

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?


None

Paranoia

Reply To Post Reply & Quote

Posted at: 9/13/05 04:31 PM

Paranoia DARK LEVEL 31

Sign-Up: 04/22/05

Posts: 9,119

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*

A rate of change in sanity with respect to time.

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 9/13/05 04:38 PM

Denvish DARK LEVEL 42

Sign-Up: 04/25/03

Posts: 15,933

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 - Report Abuse - -
Not around any more, see last news post.

BBS Signature

None

SupaTuna

Reply To Post Reply & Quote

Posted at: 9/13/05 05:51 PM

SupaTuna NEUTRAL LEVEL 19

Sign-Up: 06/18/04

Posts: 629

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?


None

T-H

Reply To Post Reply & Quote

Posted at: 9/13/05 05:53 PM

T-H LIGHT LEVEL 38

Sign-Up: 01/07/04

Posts: 4,903

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


All times are Eastern Standard Time (GMT -5) | Current Time: 11:19 AM

<< Back

This topic is 7 pages long. [ 1 | 2 | 3 | 4 | 5 | 6 | 7 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!