Be a Supporter!

As: Following/shooting At Mouse

  • 28,383 Views
  • 200 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:44 PM Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:45 PM Reply

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
arctichigh
arctichigh
  • Member since: Aug. 27, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:46 PM Reply

wow. that was useful.

denvish = <3

Deathcon7
Deathcon7
  • Member since: Oct. 1, 2003
  • Offline.
Forum Stats
Member
Level 21
Writer
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:46 PM Reply

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.

Wretched-Raygun
Wretched-Raygun
  • Member since: Feb. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:49 PM Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:53 PM Reply

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
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 04:54 PM Reply

Click me plz

Good tutorial =D


Sup, bitches :)

BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:03 PM Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:05 PM Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:07 PM Reply

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

Wretched-Raygun
Wretched-Raygun
  • Member since: Feb. 9, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:19 PM Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:24 PM Reply

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

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:28 PM Reply

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.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:30 PM Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:31 PM Reply

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

Shomer
Shomer
  • Member since: Jun. 23, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:44 PM Reply

i love you.

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 05:46 PM Reply

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.

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 07:19 PM Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 07:22 PM Reply

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
CUCKA-DUDE
CUCKA-DUDE
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 08:51 PM Reply

Thanks, i had already posted a game at

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

CLick the gun to shoot.

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Following/shooting At Mouse Sep. 6th, 2005 @ 08:55 PM Reply

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

NuiStyles
NuiStyles
  • Member since: Jul. 10, 2004
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Following/shooting At Mouse Sep. 7th, 2005 @ 12:21 AM Reply

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

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Following/shooting At Mouse Sep. 7th, 2005 @ 12:49 AM Reply

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.

NuiStyles
NuiStyles
  • Member since: Jul. 10, 2004
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Following/shooting At Mouse Sep. 7th, 2005 @ 01:13 AM Reply

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

Acerbic
Acerbic
  • Member since: Nov. 7, 2004
  • Offline.
Forum Stats
Member
Level 35
Blank Slate
Response to As: Following/shooting At Mouse Sep. 7th, 2005 @ 02:54 AM Reply

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

SupaTuna
SupaTuna
  • Member since: Jun. 18, 2004
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Following/shooting At Mouse Sep. 13th, 2005 @ 01:46 AM Reply

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?

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to As: Following/shooting At Mouse Sep. 13th, 2005 @ 04:31 PM Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Following/shooting At Mouse Sep. 13th, 2005 @ 04:38 PM Reply

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
SupaTuna
SupaTuna
  • Member since: Jun. 18, 2004
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Following/shooting At Mouse Sep. 13th, 2005 @ 05:51 PM Reply

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?

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Following/shooting At Mouse Sep. 13th, 2005 @ 05:53 PM Reply

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