inheritance in xna

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 5107 Views - Last Post: 01 April 2011 - 02:52 PM

#1 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

inheritance in xna

Posted 30 March 2011 - 05:44 AM

im currently trying to use the bullet manager for the bossmanager class, game wise i need for the boss to shoot at the hero however i will need to overide some methods in the bullet manager class such as the collision detection in order fo it to be succesfull. im also confused in how to calculate the position of the hero, i try use this formulae but it doesnt seem to work for me. (in order to get the direction of where the hero is )
is there something that im doing wrong with this calculation
 x3 = new Vector2( tank.position.X -bigboss.position.X);
                y4 = new Vector2( tank.position.Y-bigboss.position.Y);
                x3.Normalize();
                y4.Normalize();
              bdirection = Vector2.Add(x3,y4);


              bull.FireBullet(bigboss.position, bdirection);



also how do i go about changing specific methods within the bullet manager class from the boss class
boss class
using System;
using Microsoft.Xna.Framework;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;

namespace Tank
{
    class BossManager:BulletManager
    {
        private Texture2D texture;          // sprite texture
        private Vector2 position;           // current position on screen
        private Vector2 bdirection;          // current direction
        private Vector2 orgin;
        private Vector2 startDirection;
        private Vector2 bulldirect;
        public boss bigboss;
       public Vector2 x3;
       public Vector2 y4;


        const int START_POSITION_X = 1205;
        const int START_POSITION_Y = 275;

        private float width;
        private float height;
        private Random random;

        public bool back;
        public BossManager()
        {
            bigboss = new boss();
            random = new Random(2);
        }
        public void Initialize(Texture2D bossTexture)
        {
            bigboss.Initialize(bossTexture, random);

        }
        public void reset()
        {
          
            height = texture.Height;
            width = texture.Width;
            orgin.X = width / 2f;
            orgin.Y = height / 2f;

         
        }
        public void Update(GameTime gameTime, ref Hero tank, ref BulletManager bull)
        {
            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;
            bigboss.position.X = bigboss.position.X - ((bigboss.direction.X * bigboss.speed) * GameConstants.EnemySpeedAdjustment * timeDelta);
            if (bigboss.position.X < 990)
            {
                 
                bigboss.position.X = 990;
                bigboss.position.Y = bigboss.position.Y - ((bigboss.direction.Y * bigboss.speed) * GameConstants.EnemySpeedAdjustment * timeDelta);
                if (bigboss.position.Y < 150)
                {
                   
                    bigboss.direction.Y =- bigboss.direction.Y;
                 
              
                }
                else if (bigboss.position.Y > 600)
                {
                    bigboss.direction.Y = -bigboss.direction.Y;
                }
                x3 = new Vector2( tank.position.X -bigboss.position.X);
                y4 = new Vector2( tank.position.Y-bigboss.position.Y);
                x3.Normalize();
                y4.Normalize();
              bdirection = Vector2.Add(x3,y4);


              bull.FireBullet(bigboss.position, bdirection);

            }
        }
      




        
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(bigboss.texture, bigboss.position,null, Color.White,0f,bigboss.orgin,1f,SpriteEffects.None,0f);
        }




    }
}



bulletmanager
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace Tank
{
    class BulletManager
    {
        // the array of bullets
        private Bullets[] BulletArray;
        public ArrayList bsprites = new ArrayList();
        public BulletManager()
        {
            BulletArray = new Bullets[GameConstants.numBullets];
            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                BulletArray[i] = new Bullets();
            }

        }
        public void Intialize(Texture2D bulletTexture)
        {

            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                BulletArray[i].Initialize(bulletTexture);
            }

        }
        public void add(int x, int y, int height, int width)
        {
            bsprites.Add(new Rectangle(x, y, width, height));
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                if (BulletArray[i].alive)
                    spriteBatch.Draw(BulletArray[i].texture, BulletArray[i].position,
                        (Rectangle)bsprites[0], Color.White, 0f, BulletArray[i].origin, 1f, SpriteEffects.None, 0f);
            }
        }
        public void FireBullet(Vector2 position, Vector2 direction)
        {
            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                if (!BulletArray[i].alive)
                {
                    BulletArray[i].alive = true;
                    BulletArray[i].position = position;
                    BulletArray[i].direction = direction;
                    break;
                }

            }
        }
        public void Update(GameTime gameTime, ref EnemyManager enemies)
        {
            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;
            //for all bullets
            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                if (BulletArray[i].alive)
                {//move bullet according to the direction and speed
                    BulletArray[i].position.X = BulletArray[i].position.X + ((BulletArray[i].direction.X * BulletArray[i].speed) * GameConstants.BulletSpeedAdjustment * timeDelta);
                    BulletArray[i].position.Y = BulletArray[i].position.Y + ((BulletArray[i].direction.Y * BulletArray[i].speed) * GameConstants.BulletSpeedAdjustment * timeDelta);
                    //if the bullet goes off screen then its not alive
                    if (BulletArray[i].position.X < 0 || BulletArray[i].position.X > GameConstants.ScreenWidth || BulletArray[i].position.Y < 0 || BulletArray[i].position.Y > GameConstants.ScreenHieght)
                    {
                        BulletArray[i].alive = false;
                    }


                }
                if (BulletArray[i].alive)
                {
                    for (int j = 0; j < GameConstants.numBullets; j++)
                    {// if an enemy is alive
                        if (enemies.enemyArray[j].ealive)
                        {// if the bullet and the enemy collide
                            if (Helper.CollideMidPointRectangles(
                                BulletArray[i].position, BulletArray[i].width, BulletArray[i].height,
                                enemies.enemyArray[j].position, enemies.enemyArray[j].width, enemies.enemyArray[j].height))
                            {   // orgins
                                BulletArray[i].alive = false;
                                // set the enemy to be not alive
                                enemies.enemyArray[j].ealive = false;
                                //decrement the number of enemiesLeftThisLevel


                                //enemies.enemiesLeftThisLevel--;
                                //// increment the score
                                //score++;
                                //// create a small explosion at this position
                                //explosions.CreateExplosion(enemies.enemyArray[j].position,
                                //    GameConstants.SmallScaleExplosionInc);
                            }
                        }
                    }
                }




            }

        }
    }
}


This post has been edited by skyerz: 30 March 2011 - 05:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: inheritance in xna

#2 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: inheritance in xna

Posted 30 March 2011 - 07:57 AM

to overload an inherited method just write a method with the same name and parameters but put the keyword new before it.

Also you are creating two vector2s with an x value but no y value in the first section, so when you add them it will come out with a vector with x and no y
Was This Post Helpful? 1
  • +
  • -

#3 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 30 March 2011 - 08:15 AM

when i did do overload it came up with an error, both of them are set ot public

Error 2 'Tank.BossManager.Update(Microsoft.Xna.Framework.GameTime, ref Tank.Hero, ref Tank.bossbullmanager)' is inaccessible due to its protection level C:\Documents and Settings\jl781\Desktop\Tank\Tank\Game1.cs 173 19 Tank

also i was under the impression that i had to subtract both x points and y points in order to get the direction

This post has been edited by skyerz: 30 March 2011 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: inheritance in xna

Posted 30 March 2011 - 08:21 AM

You might want to read up on inheritance in C# here.

Also, make sure you're marking those classes as public to prevent that error.

This post has been edited by lesPaul456: 30 March 2011 - 08:22 AM

Was This Post Helpful? 1
  • +
  • -

#5 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 30 March 2011 - 12:57 PM

Thank you that tutortal proved very useful however the efforts would be in vain if im unavle to work owt the direction that i need the boss to fire in, would it be possibl;e to work out the angle then divde that by 100 to work out the direction ? (direction is 0 -1)
Was This Post Helpful? 0
  • +
  • -

#6 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: inheritance in xna

Posted 31 March 2011 - 05:11 AM

I've already pointed out that you're only giving an x direction in the vector because you are adding together two vector2 which have a 0 for y.

This may help

http://create.msdn.c...g/sample/aiming
Was This Post Helpful? 1
  • +
  • -

#7 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 31 March 2011 - 03:03 PM

Thank this has worked, here is the code if anyone has questions on it
 x3=new Vector2(tank.position.X - bigboss.position.X,tank.position.Y - bigboss.position.Y);
                //y4.Y=(tank.position.Y - bigboss.position.Y);
                x3.Normalize();
              
                

                if (frame > 50 &&bigboss.ealive==true) 
                {
                    bull.FireBullet(bigboss.position, x3);
                    frame = 0;
                }
                //  frame = 0;
                frame++;
            }
        }



i do have a problem with the health of my specific objects, it seems if i get hit wit three bullets its game over, the health is set to 100 and each bullet decremnts the value by one, even when i set a breakpoint it seems to be decremting by 1 each time
this is update method were the bullet is shot
public void Update(GameTime gameTime, ref Hero tank, ref bossbullmanager bull)
        {
            light.spotlightAngle = TurnToFace(light.spotlightPosition, tank.position, light.spotlightAngle, GameConstants.SpotlightTurnSpeed);
            light.spotlightPosition = new Vector2(bigboss.position.X, bigboss.position.Y);

            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;
            bigboss.position.X = bigboss.position.X - ((bigboss.direction.X * bigboss.speed) * GameConstants.EnemySpeedAdjustment * timeDelta);
            if (bigboss.position.X < 990)
            {

                bigboss.position.X = 990;
                bigboss.position.Y = bigboss.position.Y - ((bigboss.direction.Y * bigboss.speed) * GameConstants.EnemySpeedAdjustment * timeDelta);
                if (bigboss.position.Y < 150)
                {

                    bigboss.direction.Y = -bigboss.direction.Y;


                }
                else if (bigboss.position.Y > 600)
                {
                    bigboss.direction.Y = -bigboss.direction.Y;
                }
                x3=new Vector2(tank.position.X - bigboss.position.X,tank.position.Y - bigboss.position.Y);
                //y4.Y=(tank.position.Y - bigboss.position.Y);
                x3.Normalize();
              
                

                if (frame > 50 &&bigboss.ealive==true) 
                {
                    bull.FireBullet(bigboss.position, x3);
                    frame = 0;
                }
                //  frame = 0;
                frame++;
            }
        }

this is wear decremnt the health
 public void Update(GameTime gameTime, ref Hero tank, ref int mCHealth)
        {
            
            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;
          
            //for all bullets
            for (int i = 0; i < GameConstants.numBullets; i++)
            {
                if (bulletArray[i].alive)
                {//move bullet according to the direction and speed
                    bulletArray[i].position.X = bulletArray[i].position.X + ((bulletArray[i].direction.X * bulletArray[i].speed) * GameConstants.BulletSpeedAdjustment * timeDelta);
                    bulletArray[i].position.Y = bulletArray[i].position.Y + ((bulletArray[i].direction.Y * bulletArray[i].speed) * GameConstants.BulletSpeedAdjustment * timeDelta);
                    //if the bullet goes off screen then its not alive
                    if (bulletArray[i].position.X < 0 || bulletArray[i].position.X > GameConstants.ScreenWidth || bulletArray[i].position.Y < 0 || bulletArray[i].position.Y > GameConstants.ScreenHieght)
                    {
                        bulletArray[i].alive = false;
                    }


                }
                if (tank.alive)
                {
                    if (Helper.CollideMidPointRectangles(bulletArray[i].position, bulletArray[i].width, bulletArray[i].height,
                        tank.position, tank.width, tank.height))
                    {
                        bulletArray[i].alive = false;
                        mCHealth--;

                    }

                }
                if (tank.clonemode == true)
                {
                    if (Helper.CollideMidPointRectangles(bulletArray[i].position, bulletArray[i].width, bulletArray[i].height,
                        tank.position2, tank.width, tank.height))
                    {
                        bulletArray[i].alive = false;
                        tank.clonemode = false;
                    }
                }


This post has been edited by skyerz: 31 March 2011 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 01 April 2011 - 03:57 AM

ive realised the problem, whilst the two points are intersecting it will keep deducting, however im unsure how i can change this to while it intersects once at a time deduct.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Tank
{
    class Helper
    {
        static public void WrapScreenPosition(ref Vector2 position)
        {
            if (position.X > GameConstants.ScreenWidth*2) position.X = 0f;
            if (position.X < 0) position.X = GameConstants.ScreenWidth*2;
            if (position.Y > GameConstants.ScreenHieght) position.Y = 0f;
            if (position.Y < 0) position.Y = GameConstants.ScreenHieght;
        }
        static public bool CollideMidPointRectangles(Vector2 postionA, float widthA, float heightA, Vector2 postionB, float widthB, float heightB)
        {
            Rectangle A = new Rectangle((int)(postionA.X - widthA / 2f), (int)(postionA.Y - heightA / 2f), (int)widthA, (int)heightA);
            
            Rectangle B = new Rectangle((int)(postionB.X - widthB / 2f), (int)(postionB.Y - heightB / 2f), (int)widthB, (int)heightB);
            return (A.Intersects(B)/>);
            //     return true;
            // else
            //    return false;
        }
        static public bool CollideRectangles(Vector2 postionA, float widthA, float heightA, Vector2 postionB, float widthB, float heightB)
        {
            Rectangle A = new Rectangle((int)(postionA.X), (int)(postionA.Y), (int)widthA, (int)heightA);
            //1114 - 340 
            Rectangle B = new Rectangle((int)(postionB.X - widthB / 2f), (int)(postionB.Y - heightB / 2f), (int)widthB, (int)heightB);
            return (A.Intersects(B)/>);
        }
        //static public void WrapScreenPosition(ref Vector2 position)
        //{
        //    if (position.X > GameConstants.ScreenWidth) position.X = 0f;
        //    if (position.X < 0) position.X = GameConstants.ScreenWidth;
        //    if (position.Y > GameConstants.ScreenHieght) position.Y = 0f;
        //    if (position.Y < 0) position.Y = GameConstants.ScreenHieght;
        //}
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: inheritance in xna

Posted 01 April 2011 - 06:23 AM

Without looking at your code, I can think of two ways to have a bullet "only hit once".

A: bullet dies when it hits something:
Check to see if the bullet hit something.
Decrement the life of the something.
Remove bullet from collection of bullets. This way it is never updated or drawn again.

B: bullet has a damage value:
Check to see if the bullet hit something.
Decrement the life of the something by the damage value.
Set the damage value to 0. Every time a "hit" is detected after this, 0 damage will be caused.
Was This Post Helpful? 1
  • +
  • -

#10 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 01 April 2011 - 08:40 AM

Great i will see how option b works, also thanks robin19, i have one last question, in my main game class i have a way in whihc one could advance to the next level however, once all the enimies are killed off in the first level, they dont actually appear in the next level, how and were do i go about intialising them again for each level.
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Tank
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        // ContentManager content;
        SpriteBatch spriteBatch;
        Hero ship = new Hero();
        Texture2D tankTexture;
        Texture2D bulletTexture; Texture2D bulletTexture2;
        Texture2D enemyTexture1; Texture2D enemyTexture2;
        Texture2D powerTexture;
        Texture2D mHealth;
        Texture2D aimer;
        BackGroundManage backgroundManager = new BackGroundManage();
        BulletManager bull = new BulletManager();
        EnemyManager enimies = new EnemyManager();
        PowerUP powerUp = new PowerUP();

        BossManager bossm = new BossManager();
        bossbullmanager bossbul = new bossbullmanager();

        Vector2 offset = new Vector2(50, 50);

        Random random = new Random(1);
        Texture2D bossTexture;
        SpriteFont lucidaConsole;                               // the font used in the game
        bool firstTime = true;
        Vector2 scorePosition = new Vector2(50, 50);            // the position to draw the score
        int level = 0;     // the level we are on
        public int mCHealth = GameConstants.NumLives;

        Song title;
        bool songstart = false;




        private bool InitGraphicsMode(int iWidth, int iHeight, bool bFullScreen)
        {
            // If we aren't using a full screen mode, the height and width of the window can
            // be set to anything equal to or smaller than the actual screen size.
            if (bFullScreen == false)
            {
                if ((iWidth <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
                    && (iHeight <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
                {
                    graphics.PreferredBackBufferWidth = iWidth;
                    graphics.PreferredBackBufferHeight = iHeight;
                    graphics.IsFullScreen = bFullScreen;
                    graphics.ApplyChanges();
                    return true;
                }
            }
            else
            {
                // If we are using full screen mode, we should check to make sure that the display
                // adapter can handle the video mode we are trying to set.  To do this, we will
                // iterate thorugh the display modes supported by the adapter and check them against
                // the mode we want to set.
                foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
                {
                    // Check the width and height of each mode against the passed values
                    if ((dm.Width == iWidth) && (dm.Height == iHeight))
                    {
                        // The mode is supported, so set the buffer formats, apply changes and return
                        graphics.PreferredBackBufferWidth = iWidth;
                        graphics.PreferredBackBufferHeight = iHeight;
                        graphics.IsFullScreen = bFullScreen;
                        graphics.ApplyChanges();
                        return true;
                    }
                }
            }
            return false;
        }

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            //Content.RootDirectory = "Content";
        }
        public enum GameState
        {
            start,
            playing,
            preparelevel,
            gameover
        }
        GameState gamestate = GameState.start;


        /// <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
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            InitGraphicsMode(GameConstants.ScreenWidth, GameConstants.ScreenHieght, false);
            backgroundManager.Initialize();
            ship.Add(8, 10, 140, 100);
            ship.Add(254, 10, 140, 100);
            ship.Add(458, 10, 140, 100);
            ship.Add(682, 10, 140, 100);
            ship.Add(862, 14, 140, 100);
            bull.add(6, 7, 20, 20);
            bossbul.add(6, 7, 20, 20);

            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()
        {
            Random random1 = new Random();

            tankTexture = Content.Load<Texture2D>("Content/herotank");
            bulletTexture = Content.Load<Texture2D>("content/bullets");

            enemyTexture1 = Content.Load<Texture2D>("content/bosses");
            enemyTexture2 = Content.Load<Texture2D>("Content/normal ship");
            powerTexture = Content.Load<Texture2D>("content/power");
            mHealth = Content.Load<Texture2D>("content/HealthBar2");
            bossTexture = Content.Load<Texture2D>("content/big boss");
            aimer = Content.Load<Texture2D>("content/spotlight");
            bulletTexture2 = Content.Load<Texture2D>("content/bullets1");

            lucidaConsole = Content.Load<SpriteFont>("Content/Lucida Console");

            title = Content.Load<Song>("content/thuneral");
            MediaPlayer.IsRepeating = true;



            // Create a new SpriteBatch, which can be used to draw textures.
            //  spriteBatch = new SpriteBatch(GraphicsDevice);
            ship.Initialize(tankTexture);
            bull.Intialize(bulletTexture);
            enimies.Initialize(enemyTexture1, enemyTexture2);
            powerUp.Intialize(powerTexture);
            bossm.Initialize(bossTexture, aimer);
            bossbul.Intialize(bulletTexture2);
            backgroundManager.LoadContent(Content);


            // 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)
        {
            KeyboardState keyState = Keyboard.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (gamestate == GameState.start)
            {
                if (keyState.IsKeyDown(Keys.Space))
                {
                    level++;
                    enimies.totalEnimiesThisLevel = 10;
                    enimies.enimiesLeftThisLevel = enimies.totalEnimiesThisLevel;
                    gamestate = GameState.playing;
                }
            }
            if (gamestate == GameState.preparelevel)
            {
                if (firstTime)
                {
                    level++;
                    enimies.totalEnimiesThisLevel = 5;
                    enimies.enimiesLeftThisLevel = enimies.totalEnimiesThisLevel;
                  
                    if (enimies.totalEnimiesThisLevel > GameConstants.NumPossibleEnemies)
                    {
                        enimies.totalEnimiesThisLevel = GameConstants.NumPossibleEnemies;

                        
                        //bossm.Initialize();
                    }
                    firstTime = false; 

                }
                else if (firstTime == false)
                {
                    enimies.enemyArray.Initialize();
                }
                if (keyState.IsKeyDown(Keys.B)/>)
                    {
                        gamestate = GameState.playing;
                        firstTime = true;
                       
                    }
            }
            if (gamestate == GameState.playing)
            {


                powerUp.update(random);
                backgroundManager.Update(gameTime);
                ship.Update(gameTime, ref bull, ref enimies, ref powerUp, ref mCHealth);
                bull.Update(gameTime, ref enimies, ref bossm);
                bossm.Update(gameTime, ref ship, ref bossbul);
                bossbul.Update(gameTime, ref ship, ref mCHealth);
                enimies.Update(gameTime, ref level);

                //bossm.health = GameConstants.bosslife;
                //if (bossm.health == GameConstants.bosslife)
                //{
                //    bossm.bigboss.ealive = true;
                //}

                if (mCHealth <= 0)
                {
                    gamestate = GameState.gameover;
                }
                if (gamestate == GameState.gameover)
                {
                    if (keyState.IsKeyDown(Keys.Enter))
                    {
                        gamestate = GameState.start;
                    }
                }
                if (enimies.enimiesLeftThisLevel <= 0)
                {
                    gamestate = GameState.preparelevel;//
                }
            }
            if (!songstart)
            {
                MediaPlayer.Play(title);
                songstart = true;
            }

            // TODO: Add your update logic here

            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)
        {

            if (gamestate == GameState.start)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                spriteBatch.Begin();
                spriteBatch.DrawString(lucidaConsole, "TANK!!!!!!!!", offset, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f);
                offset.Y = 100;
                spriteBatch.DrawString(lucidaConsole, "Use A, S, W,D and Q to shoot, use arrows to move and jump",
                   offset, Color.LightGreen);
                offset.Y = 30;
                spriteBatch.DrawString(lucidaConsole, "do your best soldier",
                   offset, Color.LightGreen);
                offset.Y = 180;
                spriteBatch.DrawString(lucidaConsole, "Press Start or the S key to continue.",
                offset, Color.LightGreen);
                spriteBatch.End();
            }
            if (gamestate == GameState.preparelevel)
            {
                GraphicsDevice.Clear(Color.DarkBlue);
                spriteBatch.Begin();
                // spriteBatch.Draw(introBackground, spritePosition, Color.White);
                spriteBatch.DrawString(lucidaConsole, "Level: " + level + " \n\n " + "Enemys: " + enimies.enimiesLeftThisLevel
     + " \n\n" + " \n\n\n\n " + "Press B to continue", scorePosition, Color.LightGreen);
                spriteBatch.End();

            }
            if (gamestate == GameState.gameover)
            {
                GraphicsDevice.Clear(Color.DarkBlue);
                spriteBatch.Begin();
                // spriteBatch.Draw(introBackground, spritePosition, Color.White);
                spriteBatch.DrawString(lucidaConsole, "Game Over: " + " \n\n " + "Level: " + level
     + " \n\n\n\n " + "Press B to continue", scorePosition, Color.LightGreen);
                spriteBatch.End();

            }
            else if (gamestate == GameState.playing)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                spriteBatch.Begin();
                backgroundManager.Draw(gameTime, spriteBatch);
                bossm.Draw(gameTime, spriteBatch);
                ship.Draw(spriteBatch);
                bull.Draw(spriteBatch);
                enimies.Draw(spriteBatch);
                powerUp.Draw(spriteBatch);

                bossbul.Draw(spriteBatch);
                spriteBatch.Draw(mHealth, new Rectangle(this.window.ClientBounds.Width / 2 - mHealth.Width / 2, 30, mHealth.Width, 44),
                    new Rectangle(0, 0, mHealth.Width, 44), Color.White);

                spriteBatch.Draw(mHealth, new Rectangle(this.window.ClientBounds.Width / 2 - mHealth.Width / 2,
         30, (int)(mHealth.Width * ((double)mCHealth / 100)), 44),
         new Rectangle(0, 45, mHealth.Width, 44), Color.Red);

                spriteBatch.Draw(mHealth, new Rectangle(this.window.ClientBounds.Width / 2 - mHealth.Width / 2,
                    30, mHealth.Width, 44), new Rectangle(0, 0, mHealth.Width, 44), Color.White);
                spriteBatch.End();

                // TODO: Add your drawing code here
            }
            base.Draw(gameTime);
        }
    }
}





enemy class
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace Tank
{
    class EnemyManager
    {
        public Enemy[] enemyArray;
        private Random random;


        public int totalEnimiesThisLevel;
        public int enimiesLeftThisLevel;

        // the constructore intializes the array
        public EnemyManager()
        {
            enemyArray = new Enemy[GameConstants.NumPossibleEnemies];
            for (int i = 0; i < GameConstants.NumPossibleEnemies; i++)
            {
                enemyArray[i] = new Enemy();
            }
            random = new Random(2);
        }
        public void Initialize(Texture2D starTexture1, Texture2D starTexture2)
        {
           
            for (int i = 0; i < GameConstants.NumPossibleEnemies; i++)
            {
                if (i % 2 == 0)
                {
                    enemyArray[i].Initialize(starTexture1, random);
                }
                else
                {
                    enemyArray[i].Initialize(starTexture2, random);
                }
            }

        }

        //public void add(int x
        public void Draw(SpriteBatch spriteBatch)
        {
            // totalEnimiesThisLevel = 10;
            for (int i = 0; i < totalEnimiesThisLevel; i++)
            {
                if (enemyArray[i].ealive)
                {
                    spriteBatch.Draw(enemyArray[i].texture, enemyArray[i].position, null, Color.White,
                        0f, enemyArray[i].orgin, 1f, SpriteEffects.None, 0f);
                }
            }
        }
        public void Update(GameTime gametime, ref int level)
        {
            
            float timeDelta = (float)gametime.ElapsedGameTime.TotalSeconds;
            for (int i = 0; i < totalEnimiesThisLevel; i++)
            {
                enemyArray[i].position.X = enemyArray[i].position.X +
                   ((enemyArray[i].direction.X * enemyArray[i].speed)
                   * GameConstants.EnemySpeedAdjustment * timeDelta);
                enemyArray[i].position.Y = enemyArray[i].position.Y +
                    ((enemyArray[i].direction.Y * enemyArray[i].speed)
                    * GameConstants.EnemySpeedAdjustment * timeDelta);

                //Wrap around enemy positions when they go off the screen
                Helper.WrapScreenPosition(ref enemyArray[i].position);
                if (level < 1)
            {
                enemyArray[i].speed += 1;
            }
            }

        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: inheritance in xna

Posted 01 April 2011 - 09:17 AM

Glad you're sorted now. It is generally good manners to mark helpful posts as helpful, kind of a good way to show gratitude.
Was This Post Helpful? 1
  • +
  • -

#12 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 01 April 2011 - 09:47 AM

done done and done, is there anyway to intialise the enimes so that they appear in a new level ?
whilst debugging i have notice that the enemy alive boolean expression is set to false after the first level

This post has been edited by skyerz: 01 April 2011 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#13 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: inheritance in xna

Posted 01 April 2011 - 09:57 AM

View Postskyerz, on 01 April 2011 - 09:47 AM, said:

done done and done, is there anyway to intialise the enimes so that they appear in a new level ?


The way you need to do this is to load each level's characters from a Level class that has more than one stored set for each LevelNo that is called at the start of each new game and at the end of the game increment the LevelNo.

You may also need to reload the gameplay screen in order to reload all the content of the new level, so you will need to pass the LevelNo into the gameplay screen constructor.

This post has been edited by ShadowsEdge19: 01 April 2011 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#14 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 01 April 2011 - 10:02 AM

ive just solved my proble , creating a new method called rest then setting the enumies to alive in that method
 public void reset()
        {
            for (int i = 0; i < GameConstants.NumPossibleEnemies; i++)
            {
                enemyArray[i].ealive = true;
            }
        }

Was This Post Helpful? 0
  • +
  • -

#15 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: inheritance in xna

Posted 01 April 2011 - 11:59 AM

i have tried many differnt ways however i stil cant get everytime a bullet hits the target to increment or decremnt by one, it simply does it fo how long the intersection carries on for eventhough im setting the bullet to false
if (Helper.CollideMidPointRectangles(BulletArray[i].position, BulletArray[i].width, BulletArray[i].height, boss.bigboss.position, boss.bigboss.width, boss.bigboss.height))
                            {
                                BulletArray[i].alive = false;
                               //score++;

                            }


  static public bool CollideRectangles(Vector2 postionA, float widthA, float heightA, Vector2 postionB, float widthB, float heightB)
        {
            Rectangle A = new Rectangle((int)(postionA.X), (int)(postionA.Y), (int)widthA, (int)heightA);
            //1114 - 340 
            Rectangle B = new Rectangle((int)(postionB.X - widthB / 2f), (int)(postionB.Y - heightB / 2f), (int)widthB, (int)heightB);
            return (A.Intersects (B)/>);
        }

isit possible to do a code such as foreach (time this point collides with this point)
{
score ++;
}

This post has been edited by skyerz: 01 April 2011 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2