2016-03-19 85 views
0

我見過類似於我所問的問題,但他們並不完全給我我正在尋找的答案。我想讓5個我的精靈實例出現在屏幕上並在不同的位置產卵。由於某種原因,雖然不是5個實例,但精靈的每個「實例」都會增加屏幕上出現的一個精靈的值。(monogame)精靈繼承對象的多個實例

Sprite.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 

namespace Lab05_2__Aaron_Benjamin_ 
{ 
class Sprite 
{ 
    Texture2D mSpriteTexture; 
    public Vector2 Position; 
    Color mSpriteColor; 
    public Rectangle Size; 
    public string AssetName; 
    private float mScale = 1.0f; 

    public float Scale 
    { 
     get { return mScale; } 
     set 
     { 
      mScale = value; 
      //Recalculate the Size of the Sprite with the new scale 
      Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 
     } 
    } 

    public void LoadContent(ContentManager theContentManager, string theAssetName) 
    { 
     mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); 
     AssetName = theAssetName; 
     Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 
    } 

    public void Update(GameTime gameTime) 
    { 


    } 

    public void Draw(SpriteBatch theSpriteBatch) 
    { 
     theSpriteBatch.Draw(mSpriteTexture, Position, 
      new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
      Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0); 

     //theSpriteBatch.Draw(mSpriteTexture, Position); 
    } 
    } 
} 

Enemy.cs

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

namespace Lab05_2__Aaron_Benjamin_ 
{ 
    class Enemy : Sprite 
{ 
    const string ENEMY_ASSETNAME = "gear"; 
    Random Rand = new Random(); 
    int startPos_X; 
    int startPos_Y; 

    public void LoadContent(ContentManager theContentManager) 
    { 
     Position = new Vector2(startPos_X = Rand.Next(0 , 1000), startPos_Y = Rand.Next(0, 1000)); //= Rand.Next(0, 100),Position.Y = Rand.Next(0,100)); 

     base.LoadContent(theContentManager, ENEMY_ASSETNAME); 
    } 

    public void Update(GameTime gameTime) 
    { 
     MouseState cState = Mouse.GetState(); 


     if (Position.X > cState.X) 
     { 
      Position.X += 1; 
     } 
     if (Position.X < cState.X) 
     { 
      Position.X -= 1; 
     } 
     if (Position.Y < cState.Y) 
     { 
      Position.Y -= 1; 
     } 
     if (Position.Y > cState.Y) 
     { 
      Position.Y += 1; 
     } 

     base.Update(gameTime); 
     }   
    } 
    } 

Game1.cs

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
namespace Lab05_2__Aaron_Benjamin_ 
{ 
/// <summary> 
/// This is the main type for your game. 
/// </summary> 

public class Game1 : Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    List<Enemy> enemies = new List<Enemy>(); 




    private void CreateEnemy() 
    { 
     Enemy gear = new Enemy(); 
     Random rand = new Random(); 
     //gear = new Enemy(); 
     //gear.Position.X = rand.Next(0, 1000); 
     //gear.Position.Y = rand.Next(0, 1000); 
     Console.WriteLine(gear.Position.Y); 
     enemies.Add(gear); 

    } 

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

    /// <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 


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

     // TODO: use this.Content to load your game content here 
     for (int i = 0; i < 5; i++) 
     { 
      CreateEnemy(); 
     } 

     foreach (var cog in enemies) 
     { 
      cog.LoadContent(this.Content); 
     } 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// game-specific 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) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      Exit(); 

     // TODO: Add your update logic here 
     foreach (var cog in enemies) 
     { 
      if (cog.Position.X > Window.ClientBounds.Width - 50) 
       cog.Position.X = Window.ClientBounds.Width - 50; 
      if (cog.Position.Y > Window.ClientBounds.Height - 50) 
       cog.Position.Y = Window.ClientBounds.Height - 50; 

      if (cog.Position.X < 0) 
       cog.Position.X = 0; 
      if (cog.Position.Y < 0) 
       cog.Position.Y = 0; 
     } 

     foreach (var cog in enemies) 
     { 
      cog.Update(gameTime); 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.M)) 
     { 
      // If 'm' is down, we create a new meteor. Note that once this is working 
      // this is going to make a lot of meteors. That's another issue, though. 
      CreateEnemy(); 
     } 
     //Console.WriteLine(enemies.Count); 
     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.Black); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     foreach (var cog in enemies) 
     { 
      cog.Draw(spriteBatch); 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 
     } 
    } 
} 

回答

0

你不是應該使用齒輪在這個部分,而不是這.gear

foreach (var gear in enemies) 
{ 
     this.gear.LoadContent(this.Content); 
} 
+0

我的朋友只注意到。我將所有這些變爲齒輪而不是齒輪,並移動到創建實例的位置。它仍然只有一個精靈出現,但速度正確。有些東西告訴我隨機位置對於每個精靈來說都不是一個新的隨機位置。我會更新代碼。 – Starkium

+0

我更新了代碼。 – Starkium

0

好吧,我得到它的工作。所以我的隨機數被分配,然後在代碼的不同部分重新分配,以便它們全部堆疊在一起。這是解決方案。

Sprite.cs

class Sprite 
{ 
    Texture2D mSpriteTexture; 
    public Vector2 Position; 
    Color mSpriteColor; 
    public Rectangle Size; 
    public string AssetName; 
    private float mScale = 1.0f; 

    public float Scale 
    { 
     get { return mScale; } 
     set 
     { 
      mScale = value; 
      //Recalculate the Size of the Sprite with the new scale 
      Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 
     } 
    } 

    public void LoadContent(ContentManager theContentManager, string theAssetName) 
    { 
     mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); 
     AssetName = theAssetName; 
     Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 
    } 

    public void Update(GameTime gameTime) 
    { 


    } 

    public void Draw(SpriteBatch theSpriteBatch) 
    { 
     theSpriteBatch.Draw(mSpriteTexture, Position, 
      new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), 
      Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0); 

     //theSpriteBatch.Draw(mSpriteTexture, Position); 
    } 

    } 
} 

Enemy.cs

class Enemy : Sprite 
{ 
    const string ENEMY_ASSETNAME = "gear"; 
    Random Rand = new Random(); 
    //int startPos_X; 
    //int startPos_Y; 

    public void LoadContent(ContentManager theContentManager) 
    { 
     Position = new Vector2(Position.X ,Position.Y); 

     base.LoadContent(theContentManager, ENEMY_ASSETNAME); 
    } 

    public void Update(GameTime gameTime) 
    { 
     MouseState cState = Mouse.GetState(); 


     if (Position.X > cState.X) 
     { 
      Position.X += 1; 
     } 
     if (Position.X < cState.X) 
     { 
      Position.X -= 1; 
     } 
     if (Position.Y < cState.Y) 
     { 
      Position.Y -= 1; 
     } 
     if (Position.Y > cState.Y) 
     { 
      Position.Y += 1; 
     } 

     base.Update(gameTime); 
    }   
    } 
} 

Game1.cs

public class Game1 : Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    List<Enemy> enemies = new List<Enemy>(); 
    Random rand = new Random(); 



    private void CreateEnemy() 
    { 
     Enemy gear = new Enemy(); 

     //gear = new Enemy(); 
     gear.Position.X = rand.Next(0, 1000); 
     gear.Position.Y = rand.Next(0, 1000); 
     Console.WriteLine(gear.Position.Y); 
     enemies.Add(gear); 
     enemies[0].Position.X += 10; 

    } 

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

    /// <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 


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

     // TODO: use this.Content to load your game content here 
     for (int i = 0; i < 5; i++) 
     { 
      CreateEnemy(); 
     } 

     foreach (var cog in enemies) 
     { 
      cog.LoadContent(this.Content); 
     } 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// game-specific 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) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      Exit(); 

     // TODO: Add your update logic here 
     foreach (var cog in enemies) 
     { 
      if (cog.Position.X > Window.ClientBounds.Width - 50) 
       cog.Position.X = Window.ClientBounds.Width - 50; 
      if (cog.Position.Y > Window.ClientBounds.Height - 50) 
       cog.Position.Y = Window.ClientBounds.Height - 50; 

      if (cog.Position.X < 0) 
       cog.Position.X = 0; 
      if (cog.Position.Y < 0) 
       cog.Position.Y = 0; 
     } 

     foreach (var cog in enemies) 
     { 
      cog.Update(gameTime); 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.M)) 
     { 
      // If 'm' is down, we create a new meteor. Note that once this is working 
      // this is going to make a lot of meteors. That's another issue, though. 
      CreateEnemy(); 
     } 
     //Console.WriteLine(enemies.Count); 
     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.Black); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     foreach (var cog in enemies) 
     { 
      cog.Draw(spriteBatch); 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
    } 
}