1

我正在尝试制作游戏,我希望“敌人”或流星从右侧产生并前往左侧。我正在编写一些代码,它似乎工作得很好,但是重生失败了。他们最终产卵一个和一个真的很慢,然后过了一段时间他们根本不产卵。所有帮助将不胜感激。

这是我的代码:

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

}

4

1 回答 1

2

你的问题就在这里。你的一些流星向右移动,一些根本不动。

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

尝试使用

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

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

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

于 2012-10-29T03:32:56.477 回答