Forum Topic: [as3] Rotating Shooting Help?

(111 views • 7 replies)

This topic is 1 page long.

<< < > >>
Crying

Techno

Reply To Post Reply & Quote

Posted at: 10/5/09 10:45 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

Ok i have this.

I created this by using this code:

import flash.events.*;

var gunyscale:Number = gun.scaleY;
var power:uint = 10 
var velocityX:Number;
var velocityY:Number;
var dy:Number;
var dx:Number;
var ang:Number;
var rot:Number;

velocityX = Math.cos(gun.rotation) * power;
velocityY = Math.sin(gun.rotation) * power;

stage.addEventListener(Event.ENTER_FRAME, moveMouse);
function moveMouse(Event) {
	Mouse.hide();
}
	
stage.addEventListener(Event.ENTER_FRAME, gunRoatate)
function gunRoatate(e:Event):void
{
    //Grab the angle
    var angle:Number = Math.atan2 (mouseY - gun.y, mouseX - gun.x);
    //Rotate the gun, convert angle (in radians) to degrees.
    gun.rotation = angle * 180 / Math.PI;
    
    if (gun.rotation < -180 ||gun.rotation > 180)
        {
            stage.removeEventListener(Event.ENTER_FRAME, gunRoatate)
        }
        else
        {
            stage.addEventListener(Event.ENTER_FRAME, gunRoatate)
        }
		
stage.addEventListener(Event.ENTER_FRAME, gunFlip)
	function gunFlip(e:Event):void
{
	dx = stage.mouseX - gun.x;
	dy = stage.mouseY - gun.y;
	gun.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
	rot = Math.abs(gun.rotation);
	if(rot <= 270 && rot >= 90) {
		gun.scaleY = -gunyscale;
	} else {
		gun.scaleY = gunyscale;
	}
}
}

function bulletMove(bullet:MovieClip) {
	
	
}

As you can see, i am now having trouble firing with it. I want to make it so that when you click with a mouseDown, the bullet fires.

Another thing is getting it to keep firing while holding down the mouse. The tricky thing is the rotation. I don't understand how it is going to leave the gun at the angle.

I was told

velocityX = Math.cos(gun.rotation) * power;
velocityY = Math.sin(gun.rotation) * power;

would allow it to be fired from the angle of the gun, but i am still having trouble.

Help a brotha out.

.

BBS Signature

Crying

Techno

Reply To Post Reply & Quote

Posted at: 10/6/09 02:39 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

Still haven't found a solution... :(

.

BBS Signature

Questioning

InnerChild548

Reply To Post Reply & Quote

Posted at: 10/6/09 04:08 PM

InnerChild548 FAB LEVEL 24

Sign-Up: 08/10/07

Posts: 2,542

Maybe this could help?

THIS IS THE ORIGINAL SIG. FUCK YOU.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

BBS Signature

None

Techno

Reply To Post Reply & Quote

Posted at: 10/6/09 04:42 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

At 10/6/09 04:08 PM, InnerChild548 wrote: Maybe this could help?

Helped a tiny bit.

But as you can see, the bullets come out of the back of the gun, and shoot at a different angle.

This is the code that is giving trouble to it.

shot.x = gun.x;
shot.y = gun.y;
shot.rotation = gun.rotation;

If you can figure out why it is not lining up with it, i've tried tons of things.

Another thing is i don't want a sprite for the movieclip, and when i try to add the movieClip as a bullet, it is giving me trouble.

Thanks for that though, at least SOMETHING is firing SOMEWHERE haha.

.

BBS Signature

None

Techno

Reply To Post Reply & Quote

Posted at: 10/6/09 04:48 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

At 10/6/09 04:42 PM, Techno wrote:
At 10/6/09 04:08 PM, InnerChild548 wrote: Maybe this could help?
Helped a tiny bit.

But as you can see, the bullets come out of the back of the gun, and shoot at a different angle.

This is the code that is giving trouble to it.

shot.x = gun.x;
shot.y = gun.y;
shot.rotation = gun.rotation;

If you can figure out why it is not lining up with it, i've tried tons of things.

Another thing is i don't want a sprite for the movieclip, and when i try to add the movieClip as a bullet, it is giving me trouble.

Thanks for that though, at least SOMETHING is firing SOMEWHERE haha.

HAHA WELL FUCK ME SIDEWAYS!

shot.x = gun.x;
shot.y = gun.y;
shot.rotation = gun.rotation + 90;

I fixed it!

The only thing now is to replace the sprite with a movieClip instead, any help?

.

BBS Signature

None

Techno

Reply To Post Reply & Quote

Posted at: 10/6/09 07:01 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

At 10/6/09 04:48 PM, Techno wrote:
At 10/6/09 04:42 PM, Techno wrote:
At 10/6/09 04:08 PM, InnerChild548 wrote: Maybe this could help?
Helped a tiny bit.

But as you can see, the bullets come out of the back of the gun, and shoot at a different angle.

This is the code that is giving trouble to it.

shot.x = gun.x;
shot.y = gun.y;
shot.rotation = gun.rotation;

If you can figure out why it is not lining up with it, i've tried tons of things.

Another thing is i don't want a sprite for the movieclip, and when i try to add the movieClip as a bullet, it is giving me trouble.

Thanks for that though, at least SOMETHING is firing SOMEWHERE haha.
HAHA WELL FUCK ME SIDEWAYS!

shot.x = gun.x;
shot.y = gun.y;
shot.rotation = gun.rotation + 90;

I fixed it!

The only thing now is to replace the sprite with a movieClip instead, any help?

I have also fixed this :D

booyah

The only problem NOW is the holding mouse function, any help pleaseeeeeeeeee :(

.

BBS Signature

Happy

Archon68

Reply To Post Reply & Quote

Posted at: 10/6/09 08:20 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,431

At 10/6/09 07:01 PM, Techno wrote: booyah

The only problem NOW is the holding mouse function, any help pleaseeeeeeeeee :(

Just set a variable to TRUE onMouseDown, and FALSE to onMouseUp. Or have an interval/timer/whatever AS3 uses that is started onMouseDown, and cleared onMouseUp.

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Techno

Reply To Post Reply & Quote

Posted at: 10/6/09 08:34 PM

Techno DARK LEVEL 24

Sign-Up: 08/11/06

Posts: 4,934

At 10/6/09 08:20 PM, Archon68 wrote:
At 10/6/09 07:01 PM, Techno wrote: booyah

The only problem NOW is the holding mouse function, any help pleaseeeeeeeeee :(
Just set a variable to TRUE onMouseDown, and FALSE to onMouseUp. Or have an interval/timer/whatever AS3 uses that is started onMouseDown, and cleared onMouseUp.

A whats-a-whos-it?

Pry do tell

.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 05:34 AM

<< Back

This topic is 1 page long.

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