XNA产生了多个敌人

时间:2015-06-23 07:10:39

标签: c# xna

好的,我确定我只是错过了一些非常简单的东西,因为我无法在任何地方找到答案。我正在进行一场无限的亚军比赛(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();
    }
}
}

此外,是的,我确实从互联网上获得了重力代码,我尝试过没有任何工作

2 个答案:

答案 0 :(得分:0)

在更新方法的第二个for循环中,每次都将x设置为零。这将阻止它超出该循环,这将解释你所看到的行为

答案 1 :(得分:0)

首先,这没有任何意义:

    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

}