Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

trying to understand List<>

$
0
0
hi all , i know i have posted in XNA but i feel this is just general C# so i am posting a different question here

i am trying to get my spaceship to shoot bullets,

i have created a new list which is from the bullets class i made.
 List<Bullets> bullets = new List<Bullets>();


here is my bullets class

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;
using RockRainEnhanced.Core;


namespace RockRainEnhanced
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Bullets 
    {
        protected SpriteBatch sbBatch;

        public Texture2D bulletTexture;
        public Vector2 bulletPosition;
        public int YSpeed = 10;
        public Vector2 bulletOrigin;
        public Vector2 PlayerPosition;
        public bool isVisible;
        public PlayerIndex playerID;
        Vector2 player1position;

        protected List<Bullets> bullet;

        public Bullets(Game game, ref Texture2D theTexture, PlayerIndex playerID)            
        {
            bulletTexture = theTexture;
           
            isVisible = false;

            
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
            spriteBatch.Draw(bulletTexture,bulletPosition , null, Color.White, 0f, bulletOrigin, 1f, SpriteEffects.None, 0);
            
        }
    }
}



and in my "action scene" i have passed the texture through a component from the main game1 class, and then i have a update bullets and a shoot methods in the action scene.

public void UpdateBullets()

            {
                foreach (Bullets bullet in bullets)
                {
                    newbulletposition.Y += bullet.YSpeed;
                    if (Vector2.Distance(bullet.bulletPosition, player1.playerPosition) > 500)
                        bullet.isVisible = false;
                }

                for (int i = 0; i < bullets.Count; i++)
                {
                    if (!bullets[i].isVisible)
                    {
                        bullets.RemoveAt(i);
                        i--;
                    }
                }
            }

            public void Shoot()
            {
                Bullets newBullet = new Bullets(Game, ref bulletTexture, PlayerIndex.One);
                
                newBullet.bulletPosition = Vector2.Zero ;
                newBullet.isVisible = true;


            
            }



and then in the update of action scene i am handling the input so when the user presses the "B" button on controller it plays an explosion sound(using this sound just for testing will change once i get the bullets shooting sorted) and then triggers the shoot method

GamePadState gamepadstate = GamePad.GetState(PlayerIndex.One);

                if ( oldGamePadState.Buttons.B == ButtonState.Pressed && gamepadstate.Buttons.B == ButtonState.Released)
                    audio.Explosion.Play();
                    Shoot();

                oldGamePadState = gamepadstate;
                UpdateBullets();




my problem is when i press B the explosion plays but the bullets dont appear or move or anything.

also im unsure how i made a bullets list and then when i shoot i create a new "newbullet" i dont understand how that newbullet adds to the bullet list ?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>