00:00
00:00

As3: Rotate And Shoot To Mouse

18,808 Views | 10 Replies
New Topic Respond to this Topic

As3: Rotate And Shoot To Mouse 2007-05-02 17:28:17


Hey all, having gotten Flash 9 yesterday I decided to make this tutorial.
I tried to explain the new things that changed over to Flash 9 to help you a bit
The fundamental coding (math part) remains the same, its just the Events and Listeners that are really new.
import flash.events.*;
// imports the events ie- ENTER_FRAME ect * means all of them
var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int = 10;
// size of the shot
var shotSpeed:int = 10;
// defines some variables
var triangle:Sprite = new Sprite();
/*
Ok, so you might have noticed the new Sprite class,
which is sort of a merge between a graphic and a movieClip
It cannot contain animation, but it can contain code
ergo, it is perfect for API
*/
with (triangle.graphics) {
lineStyle(1, 0x000000, 1);
beginFill(0x000000, 1);
moveTo(-10,10);
lineTo(10,10);
lineTo(0,-10);
lineTo(-10,10);

}
// draws a triangle. new to AS3 instead of mc.lineTo or w/e it is mc.graphics.lineTo
// you also might notice that alpha is from 0-1 now, as well is xscale and yscale
addChild(triangle);
triangle.x = 250;
triangle.y = 200;
// no more create empty movieclip or attach movie, we have addChild now. no depth needed

function mcFunction(event:Event):void {
// you will notice that there is no longer _xmouse, it is now mouseX
// _x also changed to x (same with y)
// Void is now a lowercase void
xd = triangle.x-stage.mouseX;
yd = triangle.y-stage.mouseY;
// finds the x and y difference of the sprite and the mouse
radAngle = Math.atan2(yd, xd);
// finds the angle in radians (flash works in radians not degrees)
triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
// uses PI because radians work with PI (not the tasty kind either)
}

stage.addEventListener(Event.ENTER_FRAME, mcFunction);
/* adds an event listener, which is new, this one is an ENTER_FRAME
stage is new, it is the closet thing to _root
also, mcFunction is the event that is happening on the enterFrame (ours make the triangle
rotate to the mouse
*/

function shootShot(event:MouseEvent):void {
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
moveTo(-size/2,-size);
lineTo(size/2,-size);
lineTo(size/2,size);
lineTo(-size/2,size);
lineTo(-size/2,-size);
}
addChild(shot);
// draws and attaches
shot.x=triangle.x;
shot.y=triangle.y;
shot.rotation = triangle.rotation;
// sets the shot x,y, and rotation to the triangles
shot.addEventListener(Event.ENTER_FRAME, moveShot);
// adds and enterFrame to the shot, to move it
}
function moveShot(event:Event) {
with (event.target) {
// with the object that called the event
x+= shotSpeed*Math.sin(rotation*(Math.PI/180));
y-= shotSpeed*Math.cos(rotation*(Math.PI/180));
// moves the shot depending on its rotation, uses build in math sin and cos functions
// remember that flash works in radians
if (x>=550||x<=0||y>=400||y<=0) {
// if the shot is out of the screen
event.target.removeEventListener(Event.ENTER_
FRAME, moveShot);
this.removeChild(DisplayObject(event.target))
;
// removes the shot sprite and the enterFrame on it
}
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, shootShot);
// adds a mouseListener, when the mouse is pressed

So I am extremely new to AS3, so if I did anything wrong, or anything could be done easier, please let me know.
I apologize for anything that Newgrounds might have messed up in the code.

Response to As3: Rotate And Shoot To Mouse 2007-05-02 17:31:09


Lol, we wrote nearly the exact same tutorial in the same few minuets - heres mine.

Oh well, theres bound to be a few doubles.

Response to As3: Rotate And Shoot To Mouse 2007-05-05 23:18:13


Thanks! Anyways i made an custom of this....

Example

Source .FLA


Fate. Strength. Intelligence.

Response to As3: Rotate And Shoot To Mouse 2007-05-08 17:55:05


What in the world am I doing wrong? I'm trying to point and shoot at the mouse, but I get an error. Everything works fine until a MOUSE_DOWN event is detected. Here's the error:

ReferenceError: Error #1069: Property ENTER_FRAME not found on flash.events.MouseEvent and there is no default value.
at Rot2Mouse_fla::MainTimeline/shoot()

Here is my code:

var xdiff:Number = 0;
var ydiff:Number = 0;
var posx:Number = 0;
var posy:Number = 0;
var shotSpeed:int = 10;

stage.addEventListener(MouseEvent.MOUSE_MOVE, rot);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);

function rot(Event) {
xdiff = mouseX-arrow_mc.x;
ydiff = mouseY-arrow_mc.y;
arrow_mc.rotation = Math.atan2(ydiff, xdiff)/(Math.PI/180) + 90;
}
function shoot(Event) {
posx = arrow_mc.x;
posy = arrow_mc.y;
var bullet = new Dot();
bullet.x = posx;
bullet.y = posy;
stage.addChild(bullet);
bullet.rotation = arrow_mc.rotation;
bullet.addEventListener(Event.ENTER_FRAME, moveDot);
}
function moveDot(Event) {
this.x += shotSpeed * Math.cos(this.rotation * (Math.PI/180));
this.y += shotSpeed * Math.sin(this.rotation * (Math.PI/180));
}

Also, does anyone know of a updateAfterEvent() in AS3.0 for the MOUSE_MOVE event?


~There is never a good excuse for doing nothing. God grants liberty to those willing to stand for it!

Response to As3: Rotate And Shoot To Mouse 2007-07-01 04:11:47


Hello. Have you noticed taht it also works without the first line ( import flash.events.*; )???
Is really necesary to import things???

THANKS

Response to As3: Rotate And Shoot To Mouse 2007-08-18 22:48:23


At 7/1/07 04:11 AM, guille221191 wrote: Hello. Have you noticed taht it also works without the first line ( import flash.events.*; )???
Is really necesary to import things???

THANKS

Yes, as3 is picky about that!


Fate. Strength. Intelligence.

Response to As3: Rotate And Shoot To Mouse 2008-03-14 16:09:35


At 8/18/07 10:48 PM, Mehrdad14 wrote:
At 7/1/07 04:11 AM, guille221191 wrote: Hello. Have you noticed taht it also works without the first line ( import flash.events.*; )???
Is really necesary to import things???

THANKS
Yes, as3 is picky about that!

picky about importing some things but obv. as you said here you dont have to. its a good programming habit anyway as many languages are more pickey than as3. but still not necessay in this or some other instances.

Response to As3: Rotate And Shoot To Mouse 2008-03-14 16:13:49


At 5/2/07 05:28 PM, Pyromaniac wrote: import flash.events.*;
// imports the events ie- ENTER_FRAME ect * means all of them

Just a small issue - the * basically means import the whole package.

ENTER_FRAME isn't actually a class in that package, either - it's a property of the Event class. Classes in the events package include:

flash.events.Event;  // Basic stuff like ENTER_FRAME
flash.events.KeyboardEvent;  // Keyboard stuff
flash.events.MouseEvent;  // Mouse stuff like press (mouse down), release (mouse up) and move
flash.events.TimerEvent;  // Events displatched by the flash.utils.Timer class

I generally like to import all my stuff individually so I can keep track of exactly what I'm using, but that's just me.


BBS Signature

Response to As3: Rotate And Shoot To Mouse 2008-03-14 17:06:43


At 5/8/07 05:55 PM, EglStrk wrote: What in the world am I doing wrong? I'm trying to point and shoot at the mouse, but I get an error. Everything works fine until a MOUSE_DOWN event is detected. Here's the error:

ReferenceError: Error #1069: Property ENTER_FRAME not found on flash.events.MouseEvent and there is no default value.
at Rot2Mouse_fla::MainTimeline/shoot()

Your problem is in your event handlers. Event handlers need to receive an instance of an event, not the actual event class name. So the arguments in all three of your handlers need to be changed. Here's an example:

function shoot( Event ): void
-becomes-
function shoot( event:MouseEvent ): void

function rot( Event ): void
function rot( event:MouseEvent ): void

function moveDot( Event ): void
function moveDot( event:Event ): void

Another tip: watch how many event listeners you are adding. You're going to run into problems with the structure you have here.


Flash Game Development Blog: { P I X E L W E L D E R S } | Coming soon: OS Wars: Winvasion!

BBS Signature

Response to As3: Rotate And Shoot To Mouse 2008-03-14 17:17:01


how would i be able to change the bullet using a key from 1-9. i dont really understand addChild so this would be a big help if i knew how to do this. basically im making a game where its like a platform wow, not that ambitious tho, and when im making a skill go im pretty sure i can add a timer to delay shots im just not sure how to switch skills by pressing 1-9 for diffferent ones. i understand how to reconize these events jsut not what to do to change the bullet instance. thanks in advance guys and good tut

Response to As3: Rotate And Shoot To Mouse 2008-07-21 20:13:45


Hey, can someone please make a post to this tutorial, about how to make the same of this, how to shot at mouse click, but without any child adds, everything with symbols. Well I will begin...
Draw a triangle,convert it to mc, give instant name "triangle". Draw a bullet, do the same thing as with the triangle just give an other instant name "shot". Put this actionscript at the first frame.

var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int = 2;
var shotSpeed:int = 10;

function mcFunction(event:Event):void {
xd = triangle.x-stage.mouseX;
yd = triangle.y-stage.mouseY;
radAngle = Math.atan2(yd, xd);
triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
}

stage.addEventListener(Event.ENTER_FRAME , mcFunction);

function shootShot(event:MouseEvent):void {
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
beginFill(0x1E1E1E, 0.5);
moveTo(-size/5,-size);
lineTo(size/5,-size);
lineTo(size/5,size);
lineTo(-size/5,size);
lineTo(-size/5,-size);
}
addChild(shot);

shot.x=triangle.x;
shot.y=triangle.y;
shot.rotation = triangle.rotation;
shot.addEventListener(Event.ENTER_FRAME, moveShot);
}
function moveShot(event:Event) {
with (event.target) {

x+= shotSpeed*Math.sin(rotation*(Math.PI/180 ));
y-= shotSpeed*Math.cos(rotation*(Math.PI/180 ));

if (x>=550||x<=0||y>=400||y<=0) {

event.target.removeEventListener(Event.E NTER_FRAME, moveShot);
this.removeChild(DisplayObject(event.tar get))
;

}
}
}
stage.addEventListener(MouseEvent.MOUSE_
DOWN, shootShot);

When you test it, you see that it uses a bullet which is an actionscripted sprite, not your mc..
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
beginFill(0x1E1E1E, 0.5);
moveTo(-size/5,-size);
lineTo(size/5,-size);
lineTo(size/5,size);
lineTo(-size/5,size);
lineTo(-size/5,-size);
}
addChild(shot);

But if you'll delete that, there will be a bug on clicking. Try to click a few times on .swf file, you'll see that your shoting object restarts at the same point... Figure out what is wrong with it.