XNA产生多个敌人

本文关键字:敌人 XNA | 更新日期: 2023-09-27 17:59:20

好吧,我确信我只是错过了一些非常简单的东西,因为我在任何地方都找不到答案。我正在制作一个无限奔跑者游戏(2d),但我可以在一开始就让一个敌人产卵,就这样。我到底错过了什么?这是代码:

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 WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    static Random enemyspawner = new Random();
    int enemyloc = enemyspawner.Next(500) + 500;
    /*GraphicsDeviceManager, SpriteBatch, Texture2D and Vector2 may be found only in XNA. They are just used for drawing objects and defining locations.*/
    GraphicsDeviceManager graphic;
    SpriteBatch SpriteBatch;
    Texture2D charr;
    int enemyspeed = 1;
    Vector2 charPos;
    bool jumping; //Is the character jumping?
    float startY, jumpspeed = 0; //startY to tell us //where it lands, jumpspeed to see how fast it jumps
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D grass;
    Texture2D enemy;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    /// <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
        charr = Content.Load<Texture2D>(@"dog_jump"); //Load image
        charPos = new Vector2(230, 415);//Char loc, X/Y
        grass = Content.Load<Texture2D>(@"grass1");
        enemy = Content.Load<Texture2D>(@"fire hydrant");
        startY = charPos.Y;//Starting position
        jumping = false;//Init jumping to false
        jumpspeed = 0;//Default no speed
        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
    }
    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all 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)
    {
        for (int x = 0; x == 1000; x++ )
        {
            enemyspeed++;
        }
        for (int x = 0; x == 75; x++)
        {
            x = 0;
            spriteBatch.Begin();
            spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
            spriteBatch.End();
            enemyloc = enemyspawner.Next(500);
        }
        enemyloc -= enemyspeed;
        //Init keyboard
KeyboardState keyState = Keyboard.GetState();
if (jumping)
{
charPos.Y += jumpspeed;//Making it go up
jumpspeed += 1;//Some math (explained later)
    if (charPos.Y >= startY)
    //If it's farther than ground
    {
        charPos.Y = startY;//Then set it on
           jumping = false;
    }
}
else
{
if (keyState.IsKeyDown(Keys.Space)) 
{
    jumping = true;
    jumpspeed = -12;//Give it upward thrust
}
}
        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.CornflowerBlue);
        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw(grass, new Vector2(0, 450), Color.White);
        spriteBatch.Draw(charr, charPos, Color.White);
        spriteBatch.End();
        Spawner();
        base.Draw(gameTime);
    }
    public void Spawner()
    {
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, new Vector2(enemyloc, 450 - 50),      Color.White);
        spriteBatch.End();
    }
}
}

此外,是的,我确实从互联网上得到了te gravity代码,我尝试过的任何东西都不能工作

XNA产生多个敌人

在update方法的第二个for循环中,每次都将x设置为零。这将防止它脱离该循环,这将解释您所看到的

的行为

首先,这没有任何意义:

    for (int x = 0; x == 1000; x++ )
    {
        enemyspeed++;
    }

这只会在x等于1000时增加速度。如果你去掉for循环,你会得到同样的结果,但你可能想要这个:for (int x = 0; x <1000; x++ )//increases speed 1000 times

这里还有一个问题:

    for (int x = 0; x == 75; x++)//same here as mentioned above
    {
        x = 0;
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
        spriteBatch.End();
        enemyloc = enemyspawner.Next(500);
    }

此外,您将x设置为0,因此此循环将永远运行。

编辑:

for (int x = 0; x == 1000; x++ )

我又看了一遍,我找不出任何理由让你用for循环把速度提高1000倍。所以我相信/假设你想在1000次更新后更新速度?在这种情况下,这就是你想要的:

//Add a atrubit count to your class:
int count = 0;
//Update method:
protected override void Update(GameTime gameTime)
{
    count++;//update count
    if(count % 1000 == 0){
        enemyspeed++;//update speed after 1000 updates
    }
    if(count % 75 == 0){//draw after 75 updates
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
        spriteBatch.End();
        enemyloc = enemyspawner.Next(500);
    }
    //do your other stuff
}