9 Replies - 941 Views - Last Post: 11 December 2013 - 07:17 PM

#1 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

C# xnaI can"t get bullet to shoot from Character

Posted 08 December 2013 - 06:59 PM

I can get my bullet to shoot but it starts at the characters original start position instead of the current position,
How do I find the current position of the character and make it start there? I have a character class called "player" and a separate bullet class.

  protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
         

            // create bullet
            bullet = new Bullet(Content.Load<Texture2D>("bullet"),new Vector2(player.position.X,player.position.Y), bulletDamage);


Is This A Good Question/Topic? 0
  • +

Replies To: C# xnaI can"t get bullet to shoot from Character

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C# xnaI can"t get bullet to shoot from Character

Posted 08 December 2013 - 10:41 PM

Are you correctly updating the player's position when you move him??, Can you post that code segment please?

EDIT:

I forgot that loadcontent is called once when the application is first launched (unless you explicitly invoke it). So the bullet you create will always have the player's initial position. In general your loadcontent method should contain code that loads game resources like sounds, textures, etc. For game objects like Player, Bullet, etc. It's better to do so in the constructor or the initialize method.

This post has been edited by mostyfriedman: 08 December 2013 - 11:14 PM

Was This Post Helpful? 2
  • +
  • -

#3 tHc  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 62
  • Joined: 12-October 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 09 December 2013 - 01:15 AM

View Postmostyfriedman, on 08 December 2013 - 10:41 PM, said:

Are you correctly updating the player's position when you move him??, Can you post that code segment please?

EDIT:

I forgot that loadcontent is called once when the application is first launched (unless you explicitly invoke it). So the bullet you create will always have the player's initial position. In general your loadcontent method should contain code that loads game resources like sounds, textures, etc. For game objects like Player, Bullet, etc. It's better to do so in the constructor or the initialize method.



This above.

When you fire your bullet create it that method/function.
Was This Post Helpful? 1
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: C# xnaI can"t get bullet to shoot from Character

Posted 09 December 2013 - 05:19 AM

As tHc said. Initialize the bullet when you fire so that it would get the player's current position.
Was This Post Helpful? 0
  • +
  • -

#5 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 09 December 2013 - 11:34 AM

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;




namespace RobotGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //background
        Texture2D background;
        Rectangle viewportRect;

        //heads up display
        HUD hud;

        //bullet
        Bullet bullet;
        const int bulletDamage = 100;
        //foreground
        Texture2D foreground;
        Rectangle viewportRect2;

        const int windowWidth = 800;
        const int windowHeight = 600;

        Player player;
       
   

        // Sound effects
        SoundEffect effect;
        SoundEffect shotEffect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // set resolution to 800 x 600
            graphics.PreferredBackBufferWidth = windowWidth;
            graphics.PreferredBackBufferHeight = windowHeight;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            player = new Player(Content.Load<Texture2D>("new3"), new Vector2(100, 353), 145, 120);
            enemy = new Enemy(Content.Load<Texture2D>("pawnbot"), new Vector2(300, 353), 145, 120);


         
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        /// 
       
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
         
            //sound effects
            effect = Content.Load<SoundEffect>("jump");
            shotEffect = Content.Load<SoundEffect>("shot");

            //background load
            background = Content.Load<Texture2D>("background");
           viewportRect = new Rectangle(0,0,graphics.GraphicsDevice.Viewport.Width,graphics.GraphicsDevice.Viewport.Height);


           // create bullet
           bullet = new Bullet(Content.Load<Texture2D>("bullet"), new Vector2(player.position.X, player.position.Y), bulletDamage);


           //score keeper
           hud = new HUD();
           hud.Font = Content.Load<SpriteFont>("Arial");


        
           //foreground load
           foreground = Content.Load<Texture2D>("foreground");
           viewportRect2 = new Rectangle(0,423, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

           

            
            bullet.Update(gameTime, effect);
            enemy.Update(gameTime, effect);
            player.Update(gameTime,effect);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();


            //draw background
            spriteBatch.Draw(background, viewportRect, Color.White);


            //draw foreground
            spriteBatch.Draw(foreground, viewportRect2, Color.White);

            //Draw hero
            player.Draw(spriteBatch);

            //Draw pawn
            enemy.Draw(spriteBatch);
            //score
            hud.Draw(spriteBatch);


           
            //draw bullet
            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {

                bullet.Draw(spriteBatch);
            }


            spriteBatch.End();


            base.Draw(gameTime);
        }

        internal Enemy enemy { get; set; }
    }
}



my bullet class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RobotGame
{
    class Bullet : Player
    {
        #region Fields

        bool active = true;
        int damage;

        // drawing support and moving
        Texture2D sprite;
        Rectangle drawRectangle;
        Vector2 velocity = Vector2.Zero;
        const int bulletSpeed = 3;

        //click processing
        bool moving = false;

        #endregion

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sprite">sprite</param>
        /// <param name="location">location of center</param>
        /// <param name="damage">damage from bullet</param>
        public Bullet(Texture2D sprite, Vector2 location, int damage)           
        {
            this.sprite = sprite;
            this.damage = damage;

            // build drawrectangle
            drawRectangle = new Rectangle(
               (int)(location.X - sprite.Width / 2),
               (int)(location.Y - sprite.Height / 2),
                sprite.Width, sprite.Height);

        }
     

        #endregion



        #region Properties

        public bool Active
        {
            get {return active;}
            set {active = value;}
        }

        public Rectangle CollisionRectangle
        {
            get {return drawRectangle;}
        }

        

        public int Damage
        {
            get { return damage; }
        }
         

        #endregion

        #region Methods

        /// <summary>
        /// Updates bullet, moving and shooting
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime, SoundEffect shotEffect)
        {
            // move base on velocity
            drawRectangle.X += (int)velocity.X * gameTime.ElapsedGameTime.Milliseconds;
            drawRectangle.Y += (int)velocity.Y * gameTime.ElapsedGameTime.Milliseconds;

            // launch on click
            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {

                velocity = new Vector2(1, 0 * bulletSpeed);
                shotEffect.Play();
            }

        }




        /// <summary>
        /// Draws the bullet
        /// </summary>
        /// <param name="?">spriteBatch</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(sprite, drawRectangle, Color.White);
        }

        #endregion
    }
}



Was This Post Helpful? 0
  • +
  • -

#6 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 10 December 2013 - 06:04 AM

still can't figure out how to make it work
Was This Post Helpful? 0
  • +
  • -

#7 tHc  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 62
  • Joined: 12-October 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 10 December 2013 - 08:41 AM

This is how i did it in my space invaders clone

The fire method was not in the same class i suspect this is now your issue.


public class Bullet
    {

        // Bullet texture
        public Texture2D texture;
        // Bullet bounding rectangle
        public Rectangle rectangle;
        // Is the bullet alive or not
        public bool bulletAlive = false;

        // Constructor
        public Bullet(Texture2D newTexture)
        {
            // Set the bullet texture
            texture = newTexture;
            // Set the bullet bounding rectangle width
            rectangle.Width = texture.Width;
            // Set the bullet bounding rectangle height
            rectangle.Height = texture.Height;
        }

        // Draw method
        public void Draw(SpriteBatch spriteBatch)
        {
            // Draw the bullet
            spriteBatch.Draw(texture, rectangle, Color.White);
        }
    }



How i fired my bullet in the ship class after creating it in the initialize method.

public void Update()
        {
            // If spacebar pressed and bullet dead, fire bullet
            if (keyState.IsKeyDown(Keys.Space)  && !shipBullet.bulletAlive)
            {
                // Set bullet status to alive
                shipBullet.bulletAlive = true;
                // Set the bullets position to center of ship and on top
                shipBullet.rectangle.X = playerShip.rectangle.X + playerShip.rectangle.Width / 2 - shipBullet.texture.Width / 2;
                shipBullet.rectangle.Y = playerShip.rectangle.Y - shipBullet.texture.Height;  
                // Play sound effect
                shipShooting.Play();
            }  
            // Set bullet speed
            shipBullet.rectangle.Y -= 5;          

            // Check if bullet exits screen
            if (shipBullet.rectangle.Y < 0 - shipBullet.rectangle.Height)
            {
                // Kill bullet if exits screen
                shipBullet.bulletAlive = false;
            }
        }



If you want lots of bullets you need to do it a bit different and make a list of bullets and add/remove ect..
Was This Post Helpful? 0
  • +
  • -

#8 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 10 December 2013 - 09:04 AM

how do i create it in the initialize method?


i tried this
  bullet = new Bullet(Content.Load<Texture2D>("bullet"), new Vector2(bullet.position.X, bullet.position.Y), 10);

I get a nullreferenceexception was unhandled
Was This Post Helpful? 0
  • +
  • -

#9 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: C# xnaI can"t get bullet to shoot from Character

Posted 11 December 2013 - 07:09 PM

you're passing in the bullets x and y position into the constructor instead of the players x and y position.
Was This Post Helpful? 0
  • +
  • -

#10 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Re: C# xnaI can"t get bullet to shoot from Character

Posted 11 December 2013 - 07:17 PM

I had the players and it would use the wheren the player started but not the current position
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1