Question

I am making a flash game and I have a game character similar to this

http://us.123rf.com/400wm/400/400/chudtsankov/chudtsankov1004/chudtsankov100400927/6906625-cartoon-character-mobster-carries-weapon.jpg

The character is able to shoot when facing left,and the bullet is shot diagonally from the direction of the gun.

However, when my character faces right, the bullet is shot from the back of the head rather than the gun itself. I did realise that when the character is facing right, the registration point is on the gun but when facing left, the registration point is on the back of her head. Could that be a problem? And how do i overcome this?

Here is my code

function shootBulletBoy():void {

    var fire:myBullet2 = new myBullet2();
    fire.x = GirlHero.x;
    fire.y = GirlHero.y;
    addChild(fire);
    fire.addEventListener(Event.ENTER_FRAME, moveBullet2);
}

function moveBullet2(e:Event):void {

        if (GirlHero.currentLabel == ('left')){
  e.currentTarget.x += Math.cos(rads) * speed;  
  e.currentTarget.y += Math.sin(rads) * speed;
  e.currentTarget.rotation = 265;
  }

  if (GirlHero.currentLabel == ('right')){
  e.currentTarget.x += Math.cos(radss) * speedd;  
  e.currentTarget.y += Math.sin(radss) * speedd;
  }}

Another issue that I am having is that when my character is facing left and shoots, the gun shoots in the left direction however if the character turns right whilst the bullet is still up in the air, my bullet all of a sudden turns in the rights direction. I don't know why its doing this.

Sorry if im asking too much. You dont have to answer both my problems, a little help would do :)

Thank you

Was it helpful?

Solution

To fix your issue with the origin of your players shoot has to do with:

fire.x = GirlHero.x;
fire.y = GirlHero.y;

You are right. the registration point will be an issue. To fix this use an if statement much like your moveBullet2 function.

if (GirlHero.currentLabel == ('left')){
     fire.x = GirlHero.x + leftXOffset;
     fire.y = GirlHero.y + leftYOffset;
}else if (GirlHero.currentLabel == ('right')){
    fire.x = GirlHero.x + rightXOffset;
    fire.y = GirlHero.y + rightYOffset;
}

The offset values will need to be played with based on you character.

For you bullet shooting problem.

your MoveBullet2() function is updating direction of you bullets while they are in the air. Once a bullet is shot, you do not want to change its direction.

Instead in your myBullet2 Class (or in code inside myBullet2 MovieClip) create variables rads and rotation. Then set them when you create a bullet. Then in your moveBullet2() youd have:

var b:myBullet2 = myBullet2(e.currentTarget);
b.x += Math.cos(b.rads) * speedd;  
b.y += Math.sin(b.rads) * speedd;

and where you create bullet would add:

fire.rads = rads;
fire.rotation = rot;

which these values would be based on the direction character facing and where the mouse was pointing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top