XNA随机重生“敌人”

时间:2012-10-29 03:01:09

标签: c# 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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

你的问题就在这里。你的一些流星正在向右移动,而有些则根本没有移动。

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

尝试使用

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

你的敌人/流星有时在X上的速度在0到4之间,所以他们向右移动。

我可以看到你把Y改成了这个,也许你把它们搞砸了?