简体   繁体   English

自动绘制Texture2D,而无需重复写入多行

[英]Autodraw Texture2D without writing multiple lines repeatedly

I have tried to draw Texture2D s in a line by adding to the x and y coordinates when it gets to the end of the screen. 我试图通过在到达屏幕末端时将x和y坐标添加到一行中来绘制Texture2D The Draw method doesn't draw multiple sprites it just moves the first one at the speed of 20 instead of drawing one every 20 pixels. Draw方法不会绘制多个精灵,它只是以20的速度移动第一个精灵,而不是每20个像素绘制一个。

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

    // TODO: Add your drawing code here
    spriteBatch.Begin();
    spriteBatch.DrawString(GameText, MenuText, new Vector2(325,75), Color.Red);

    do
    {  
        spriteBatch.Draw(EarthGrass, place, Color.White);
        place.X += 20;
    } while (place.X < 800);
    place.X = -20;
    place.Y += 20;
    do
    {                
        int IDBint = IDB.Next(11);
        if (IDBint == 10)
        {
            spriteBatch.Draw(PooperMachoOre, place, Color.White);
        }
        else
        {
            spriteBatch.Draw(EarthDirt, place, Color.White); 
        }
        place.X += 20; 

    } while (place.X < 800);
    place.X = -20;
    place.Y += 20;
    spriteBatch.End();
    base.Draw(gameTime);
}

Your code looks fine at first glance, even though I barley see do loops these days. 乍看之下,您的代码看起来还不错,即使我最近看过do循环也是如此。 I assume you want to STOP drawing after the X position is more than 460 pixels, since it starts at 0. So simply flip your > to a < , you will also need to reset your X position each frame, or else it will start where it left off. 我假设你要停止拉伸后的X位置超过 460个像素,因为它从0开始。所以,简单地翻转你>< ,你还需要重置您的X位置每一帧,否则它会从哪里开始它停了下来。

do
{
     place.X += 20;
     spriteBatch.Draw(EarthGrass, place, Color.White);
} while (place.X > 460);
place.X = 0;

If you do: 如果您这样做:

do
{                
    int IDBint = IDB.Next(11);
    if (IDBint == 10)
        spriteBatch.Draw(PooperMachoOre, place, Color.White);
    else
        spriteBatch.Draw(EarthDirt, place, Color.White);

    place.X += 20;

} while (place.X < 800);

your Draw will change all the sequence of images to draw every frame (and as Cyral wrote, it happens 60 times per second). 您的Draw将更改所有图像序列以绘制每一帧(并且正如Cyral所写,每秒发生60次)。
You don't have to call your random generator in the Draw method, but you have to set the sequence of textures to draw only once. 您不必在Draw方法中调用随机生成器,但必须将纹理序列设置为仅绘制一次。 In the Initialize , for example. 例如,在Initialize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM