2012-10-29 109 views
1

我想製作一款遊戲,並且我想要「敵人」或流星從右側產卵並前往左側。我正在研究一些代碼,它似乎工作得很好,但重新生成失敗。他們最終產卵一個,一個真的很慢,過了一段時間,他們根本沒有產卵。所有的幫助將非常感激。XNA隨機重生「敵人」

這裏是我的代碼:

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

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    List<Enemies> enemies = new List<Enemies>(); 
    Random random = new Random(); 

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

    protected override void Initialize() 
    { 

     base.Initialize(); 
    } 
    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
    } 

    protected override void UnloadContent() 
    { 
    } 

    float spawn = 0; 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     spawn += (float)gameTime.ElapsedGameTime.TotalSeconds; 

     foreach (Enemies enemy in enemies) 
     { 
      enemy.Update(graphics.GraphicsDevice); 
     } 
     LoadEnemies(); 

     base.Update(gameTime); 
    } 

    public void LoadEnemies() 
    { 
     int randY = random.Next(100, 400); 
     if (spawn > 1) 
     { 
      spawn = 0; 
      if (enemies.Count() < 10) 
       enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY))); 
     } 
     for (int i = 0; i < enemies.Count; i++) 
     { 
      if (!enemies[i].isVisible) 
      { 
       enemies.RemoveAt(i); 
       i--; 
      } 
     } 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 
     foreach (Enemies enemy in enemies) 
     { 
      enemy.Draw(spriteBatch); 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
} 

,這裏是我的課:

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

namespace WindowsGame2 
{ 
class Enemies 
{ 
    public Texture2D texture; 
    public Vector2 position; 
    public Vector2 velocity; 
    public bool isVisible = true; 

    Random random = new Random(); 
    int randX, randY; 

    public Enemies(Texture2D newTexture, Vector2 newPosition) 
    { 
     texture = newTexture; 
     position = newPosition; 

     randX = random.Next(-4, 4); 
     randY = random.Next(-4, -1); 
     velocity = new Vector2(randX, randY); 
    } 

    public void Update(GraphicsDevice graphics) 
    { 
     position += velocity; 

     if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height) 
     { 
      velocity.Y = -velocity.Y; 
     } 

     if (position.X < 0 - texture.Width) 
     { 
      isVisible = false; 
     } 
    } 
    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, Color.White); 
    } 
} 

}

+0

這是很多代碼,你可以把它裁減到相關的東西? –

+0

對,對不起。儘管我現在得到了幫助。直到下次我會記住不粘貼整個代碼,對不起! – user1775668

回答

2

你的問題就出在這裏。你的一些流星正在向右移動,有些並不動。

randX = random.Next(-4, 4); 

嘗試使用

randX = random.Next(-4, -1); 

你的敵人/流星有時對,這是0和4之間的X的速度,所以他們向右移動。

我可以看到你改變了Y這個,也許你讓他們混合起來?

+0

哦,我完全沒有想到這一點。嘆息,有時候解決方案可以非常簡單。感謝您的幫助,非常感謝! – user1775668