Jump to content
  • Advertisement
Sign in to follow this  
zoogy1983

Making a sprite shoot a bullet randomly

This topic is 1328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,
 
I am having a small issue, I have two ships spawning on the screen at one time and I want to have them shoot every few seconds.
 
I am able to pass the bullets to the enemies position but when I want it to shoot it doesn't. Posted below is my code. Any help on this matter would be great.
 

public class Enemy : GameObject
    {
        Random random = new Random();

        public Texture2D texture;
        Texture2D bullet;
        Vector2 bulletPosition;

        bool isVisible = true;
        bool bulletIsVisible = false;

        int randX, randY;
        int bulletTimer;

        public Enemy(Texture2D newTexture, Vector2 newPosition)
            : base()
        {
            texture = newTexture;
            position = newPosition;

            //Controls the speed of enemy ships
            randY = random.Next(-3, 2);
            randX = random.Next(-4, -2);
            bullet = gameManager.Game.Content.Load<Texture2D>("bullet");
            velocity = new Vector2(randX, randY);
        }

        new public void Update()
        {
            position += velocity;
            if (position.Y <= 0 || position.Y >= gameManager.Game.GraphicsDevice.Viewport.Height - texture.Height)
                velocity.Y = -velocity.Y;

            if (position.X < 0 - texture.Width)
                isVisible = false;

            bulletTimer += 1;

            if (bulletTimer == 100)
            {
                bulletIsVisible = true;

            }

        }

        new public void Draw()
        {
            if (bulletIsVisible)
            {
                SpriteBatch.Draw(bullet, position, Color.White);
                bulletPosition = position;
                bulletPosition.X -= 1.0f;
            }
            SpriteBatch.Draw(texture, position, Color.White);
        }

    }

Share this post


Link to post
Share on other sites
Advertisement

In the following code:

            if (bulletIsVisible)
            {
                SpriteBatch.Draw(bullet, position, Color.White);
                bulletPosition = position;
                bulletPosition.X -= 1.0f;
            }
            SpriteBatch.Draw(texture, position, Color.White);

it appears the variable bulletPosition isn't used. At least, I don't see the variable bulletPosition elsewhere in your code. So you continually draw the bullet at the object's position, and then overdraw "texture" at the same position.

Edited by Buckeye

Share this post


Link to post
Share on other sites

Thanks for the reply!

 

When I do this:

new public void Draw()
        {
            if (bulletIsVisible)
            {
                SpriteBatch.Draw(bullet, bulletPosition, Color.White);
                bulletPosition.X -= 1.0f;
            }
            SpriteBatch.Draw(texture, position, Color.White);
        }

The bullet gets drawn at the location of where the ships spawn. What I am trying to do is give it the ships position and shoot it from where the ship is currently on the screen instead of the place where it spawned from. When step through the code bullet position has the same coordinates as enemy position so to me this should work but it doesn't. Is there another way I should be approaching this?

Share this post


Link to post
Share on other sites

Buckeye,

 

I was wrong... I was calling bulletPosition = position; in the wrong place. I am calling it in my Update method when I want to show the bullet. All works now! Thanks for the help!!

 

GO BUCKEYES!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!