Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums [AS3] Calculating angle for shooting bullets
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 12-09-2010, 03:27 PM #1 bastrix Registered User   Join Date: Sep 2010 Posts: 34 [AS3] Calculating angle for shooting bullets Hi all, This is what im trying to do: I have a character in middle of the screen that I can move around. The idea is that I can aim with the mouse, so wherever the mouse is pointing the character will shot a bullet in that direction until its out of the screen. I've done the maths before where I had the rotation and I could calculate how to move an object depending on the actual rotation value. But how would I do this if I only have the Cords of my character and the mouseX and mouseY cords of where I just clicked? Any idea? Thanks Last edited by bastrix; 12-09-2010 at 03:32 PM.
 12-09-2010, 04:25 PM #2 hey Senior Member   Join Date: Jul 2009 Location: PA Posts: 132 Something like this: ActionScript Code: ```dx = object.x - mouseX; dy = object.y - mouseY; Radians = Math.atan2(dy, dx);``` Then you can have the projectile move with something like: ActionScript Code: ```Projectile.x += Math.cos(Radians) * 5; Projectile.y += Math.sin(Radians) * 5;``` Last edited by hey; 12-09-2010 at 04:29 PM.
 12-09-2010, 04:27 PM #3 lordofduct Senior Member     Join Date: Feb 2008 Location: West Palm Beach, FL Posts: 3,883 trig yo... The specific function you want is ArcTangent, and there is a nice arctan that solves the repeating aspect of it (because top and bottom can resolve the same) Math.atan2(j, i) where j and i are the members of the vector pointing from the player to the mouse. j = mouseY - player.y i = mouseX - player.x __________________ www.lordofduct.com - come read my blog! If you want to know how to program, take a math class, take a lot of math classes!
 12-09-2010, 05:15 PM #4 bastrix Registered User   Join Date: Sep 2010 Posts: 34 Thats what I needed... thanks! I think I'm gonna have go through a few trigonometry tutorials ...
 12-09-2010, 05:23 PM #5 northmantif dont Re Member   Join Date: May 2009 Location: Poland Posts: 739 ActionScript Code: ```var speed:Number = 10; var bull:MovieClip;//represents bullet dynamically adding from library; var bulletAngle; addEventListener(Event.ENTER_FRAME, moving); function moving(e:Event):void {         mc.rotation = Math.atan2( mc.y -  mouseY,  mc.x - mouseX) / Math.PI * 180 -90;         //mc represents something that should fire the bullet (cannon, soldier etc.)         //and in my case it is a movieClip placed manually on stage } stage.addEventListener(MouseEvent.CLICK, shot); function shot(e:MouseEvent):void {          bull = new Bullet();    //new instance of Bullet                addChild(bull);         bull.x = mc.x         bull.y = mc.y;         bull.rotation = (mc.rotation -90)* Math.PI/180;         bulletAngle = bull.rotation ;         bull.addEventListener(Event.ENTER_FRAME, movingBull); } function movingBull(e:Event):void {     e.target.x += Math.cos(bulletAngle)*speed;     e.target.y += Math.sin(bulletAngle)*speed; }``` that's what I use __________________ colorful websites,webamp - flash mp3 player

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 10:27 PM.

///