Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec XNA

Plusieurs cibles en meme temps

    24 mars 2012 à 21:22:08

    Bonjour,

    Voila je débute sur XNA, et j'ai tenté de creer un petit programme afin d'afficher un nombre X de cible a l'écran.

    J'ai donc une classe Cible :

    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 ShootGame
    {
        class Cible
        {
            public Vector2 position { get; set; } // Position de la cible
            public short maxWidth { get; private set; } // Position maximal que l'on pourra atteindre quand on fera bouger la cible
            public short maxHeight { get; private set; } // Position maximal que l'on pourra atteindre quand on fera bouger la cible
            public Texture2D texture { get; set; } // Texture de la cible
    
            public Cible(int MW, int MH)
            {
                int aleaX = new Random().Next(1, (MW-100));
                int aleaY = new Random().Next(1, (MH-100));
                this.position = new Vector2(aleaX, aleaY); // On met la cible a une position aléatoire
            }
            public void Draw(SpriteBatch sprite)
            {
                sprite.Draw(this.texture, this.position, Color.White); // On affiche la cible
            }
        }
    }
    


    Ensuite, dans mon Game.cs, j'ai configuré le code de sorte que les cibles soit stocker dans une liste, et que l'on puisse donc en ajouter ou en enlever facilement lorsqu'elle seront toucher

    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 ShootGame
    {
        public class Game : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            List<Cible> cibles;
    
            public Game()
            {
                this.graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
                this.cibles = new List<Cible>();
    
                base.Initialize();
            }
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                
                this.cibles.Add(new Cible(this.graphics.PreferredBackBufferWidth, this.graphics.PreferredBackBufferHeight));
    
                foreach(Cible C in this.cibles)
                    C.texture = base.Content.Load<Texture2D>("tank_body");
            }
            protected override void UnloadContent()
            {
                foreach (Cible C in this.cibles)
                    C.texture.Dispose();
            }
            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
    
                base.Update(gameTime);
            }
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                this.spriteBatch.Begin();
                
                foreach (Cible C in this.cibles)
                    C.Draw(spriteBatch);
    
                this.spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    


    Jusque la, ça marche bien, j'ai bien une colonne de la liste contenant une cible, que j'initialise et affiche.

    Mais quand je tente d’ajouter une deuxième cible en remplaçant la methode LoadContent par :

    protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                
                this.cibles.Add(new Cible(this.graphics.PreferredBackBufferWidth, this.graphics.PreferredBackBufferHeight));
                this.cibles.Add(new Cible(this.graphics.PreferredBackBufferWidth, this.graphics.PreferredBackBufferHeight)); // on initialise une seconde cible
    
                foreach(Cible C in this.cibles)
                    C.texture = base.Content.Load<Texture2D>("tank_body");
    
                // TODO: use this.Content to load your game content here
            }
    


    Et bien la, je n'ai toujours qu'une cible a l'écran, la seconde cible de la liste ne c'est pas affiché

    Une idée du pourquoi ?
    • Partager sur Facebook
    • Partager sur Twitter
      25 mars 2012 à 12:29:01

      Parce qu'elles sont superposées exactement au même endroit... parce que tu crées un nouvel objet Random à chaque nombre que tu tires au lieu de réutiliser le même pour toutes tes cibles. Fais-en une variable statique privée. :)

      Note au passage, ta méthode UnloadContent est inutile ici : elle est prévue pour libérer les (rares) ressources non managées, or les textures sont managées donc tu n'as pas besoin de t'en occuper.

      Note 2 : pour obtenir la dimension de l'écran, utilise plutôt GraphicsDevice.Viewport.Width et GraphicsDevice.Viewport.Height.
      • Partager sur Facebook
      • Partager sur Twitter

      Problème avec XNA

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown