Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rotating guns and shooting them at the same time?

Explorer ,
Jan 04, 2015 Jan 04, 2015

I put the registration point as the gun's arms. So then the gun would rotate from the arms. But then the bullets come out from the registration point as well. So if I make the registration point on the barrel of the gun, the gun would rotate from the barrel and then shoot from the barrel. So I'm a bit confused where is the right place to place the registration point.

Now what I want would be: Shooting the bullets from the barrel, not the arms. And also rotating by placing an axis on the arms, not the barrel. At the same time.

I tried tweeking it like, bullet.x = x - 100 to make the bullet placed on the barrel and come out from there, but if i rotate let's say (180 degrees) it shoots from the from x - 100 but then the gun is on the other side, so the bullet wouldn't be coming out from the barrel anymore.

I also tried tweeking it by adding a movieclip INSIDE the movieclip gun's barrel so the bullets would come from THAT location, but it shot bullets weirdly and not in the right position.

here is my code, is there any modifications i can put? so it can shoot from the barrel regardless of rotating the gun and changing positions, and also rotate from the arms at the same time? if there is any other solution to tweek it, thanks a lot.

  function initialize(e:Event)

  {

   //add a click listener to the stage

   stage.addEventListener(MouseEvent.CLICK, fire);

  }

 

  function fire(m:Event)

  {

   //spawn a bullet

   var b = new Bullet;

   //set the position and the rotation of the bullet

   b.rotation = rotation;

   b.x = x;

   b.y = y;

   //add the bullet to the parent object

   parent.addChild(b);

   //play the firing animation

   play();

  }

 

  function update(e:Event)

  {

   //make the gun face the mouse

   if (parent != null)

   {

    var dx = parent.mouseX - x;

    var dy = parent.mouseY - y;

    var angle = Math.atan2(dy, dx)/Math.PI * 180

    rotation = angle;

   }

  }

Thank you for reading, sorry if it was long.

TOPICS
ActionScript
847
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2015 Jan 04, 2015

put the reg point so your gun transforms (ie, rotates) the way you want.  that would usually be with the breech at 0,0 and the muzzle at gun.length,0 (ie, pointing to the right).  then adjust the bullets start position:

b.x=platform.x+Math.cos(gun.rotation*Math.PI/180)*gun.length;  // where platform is the parent of the gun (assuming your gun is attached to a platform like a tank).

b.y=platform.y+Math.sin(gun.rotation*Math.PI/180)*gun.length;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 04, 2015 Jan 04, 2015

For some reason, the gun's bullets still come out from the registration point. The registration points are on the arms, the gun is facing right and the barrel is at the end of the right.

But I tried this code but the bullet comes out from the same place of the rotating axis. I tried playing around with the code but no possible solutions.

Thanks.

EDIT: Also, if I put

b.x=platform.x+Math.cos(gun.rotation*Math.PI/180)*gun.length;  // where platform is the parent of the gun (assuming your gun is attached to a platform like a tank).

b.y=platform.y+Math.sin(gun.rotation*Math.PI/180)*gun.length;

The bullet comes out from the platform's registration point. If I remove the platform part, it comes out from the gun's reg point.

Another thing, if I modify it like "length - 100", it will change it's position but won't follow the rotation of the gun like I stated above. If it shoots from a certain location., and then I rotate the gun a certain amount of degrees the bullet will shoot from that side instead of following the gun's barrel.

So I'm wasn't completely sure on how to fix this. Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2015 Jan 05, 2015

what do you mean by the gun's 'arms'?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2015 Jan 05, 2015

Oh, I keep the gun's rotating point where the player's arms are. So the gun rotates at the arms.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2015 Jan 06, 2015

then the platform is the arms and, if the arms are attached to something moveable (eg, a body), plus the body.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2015 Jan 06, 2015

Well if I use your code you showed me. It still shoots from the reg point. I attach the gun to a platform. Then the gun will shoot from the platform's reg point. If I dettach it from the platform. The gun will shoot from it's own reg point. It doesn't come out from the barrel for some reason.


I did try changing the code like into:

b.x=platform.x+Math.cos(gun.rotation*Math.PI/180)*gun.width;
b.y=platform.y+Math.sin(gun.rotation*Math.PI/180)*gun.width;


It worked half way. Changing the length to width. But it only comes out of the barrel if you rotate and face it sideways(left/right). If you shoot when rotating and facing it vertically(upwards/downwards), it comes from the middle/backside of the gun.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2015 Jan 07, 2015
LATEST

someone will probably need to check your fla

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines