【问题标题】:Drawing Multiple Sprites with Different Properties in XNA 4.0在 XNA 4.0 中绘制多个具有不同属性的精灵
【发布时间】:2016-07-07 07:33:51
【问题描述】:

我正在编写一些代码,通过创建一个 sprite 类将一百个相同的 sprite 副本动画到屏幕上。我希望精灵,一个四环互锁环相互旋转的动画,被绘制在一定高度,下降到屏幕上,像球一样弹跳,每次弹跳逐渐减少,直到它们完全停止。我已经设法完成了这部分;但是,我似乎无法找到一种方法来随机化每个不同精灵的加速度和动画速度。有人可以对我的代码提供一些建议吗?

游戏 1.cs

namespace lab_6
  {

   public class Game1 : Microsoft.Xna.Framework.Game
   {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Sprite rings;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 600;
        graphics.PreferredBackBufferWidth = 800;
    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);


        Texture2D rings_texture = Content.Load<Texture2D>("Images/threerings");

        //animation
        Point frameSize = new Point(75, 75);
        Point currentFrame = new Point(0, 0);
        Point sheetSize = new Point(6, 8);
        int millisecondsPerFrame = 50;
    rings = new Sprite(rings_texture, ringsPos,
            frameSize, 0, currentFrame, sheetSize, ringsSpeed, millisecondsPerFrame);
    }



    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();
        // TODO: Add your update logic here
        rings.Update(gameTime, Window.ClientBounds);

        base.Update(gameTime);
    }


    /// <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();

        rings.Draw(gameTime, spriteBatch);
        spriteBatch.End();
    }
   }
}

Sprite.cs

namespace lab_6
{
    public class Sprite
    {
    //basics
    protected Texture2D rings;
    protected Vector2 ringsPos = new Vector2(0,0);
    protected Color tint = Color.White;
    protected Vector2 ringsSpeed = new Vector2(0,0);
    protected Vector2 ringsAccel = new Vector2(0, 1);

    //animation
    protected Point frameSize = new Point(75,75);
    protected Point currentFrame = new Point(0, 0);
    protected Point sheetSize = new Point(6,8);

    //animation timing
    protected int timeSinceLastFrame = 0;
    protected int millisecondsPerFrame = 50;
    const int defaultMillisecondsPerFrame = 16;

    //bounding box offset
    protected int collisionOffset;

    Random r = new Random(DateTime.Now.Millisecond);

    public Sprite(Texture2D rings, Vector2 ringsPos, Point frameSize,
                    int collisionOffset, Point currentFrame, Point sheetSize, Vector2 ringsSpeed,
                    int millisecondsPerFrame)
    {
        this.rings = rings;
        this.ringsPos = ringsPos;
        this.frameSize = frameSize;
        this.collisionOffset = collisionOffset;
        this.currentFrame = currentFrame;
        this.sheetSize = sheetSize;
        this.ringsSpeed = ringsSpeed;
        this.millisecondsPerFrame = millisecondsPerFrame;
    }




    public virtual void Update(GameTime gameTime, Rectangle clientBounds)
    {


        int maxY = (600 - frameSize.Y );
        ringsAccel.Y += (byte)r.Next((1 / 10), 1);
        ringsSpeed.Y += ringsAccel.Y;
        ringsPos.Y += ringsSpeed.Y;


        if (ringsPos.Y > maxY)
        {
            ringsSpeed *= -0.8f;
            ringsPos.Y = maxY;
        }

        //Update animation frame
        millisecondsPerFrame = 50;
        millisecondsPerFrame *= ((byte)r.Next(1, 10));

        timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame = 0;
            ++currentFrame.X;
            if (currentFrame.X >= sheetSize.X)
            {
                currentFrame.X = 0;
                ++currentFrame.Y;
                if (currentFrame.Y >= sheetSize.Y)
                    currentFrame.Y = 0;
            }
        }

    }



    public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        for(int i = 0; i < 100; )
        {

           Vector2 newPos = ringsPos + new Vector2((10 * i), (1 * (byte)r.Next((1/10), 1)));
            spriteBatch.Draw(rings, newPos,
                new Rectangle(currentFrame.X * frameSize.X, 
                currentFrame.Y * frameSize.Y,
                frameSize.X, frameSize.Y),
                tint, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            i++;
            r = new Random(DateTime.Now.Second);
        }
    }
   }
}

【问题讨论】:

    标签: c# visual-studio xna


    【解决方案1】:

    将 ringSpeed 和 ringAccel 的构造函数移动到构造函数并在那里随机化它们。

    【讨论】:

    • 请详细说明您的问题。
    • 我试图让 100 个精灵中的每一个都有不同的加速度,所以当它们弹跳时,它们会以不同的速度移动。我也在尝试随机化变量millisecondsPerFrame 以赋予精灵随机动画速度。我只是不知道该怎么做。而且我不完全确定我是否遵循您的建议...我是在 Sprite.cs 文件还是 Game 1.cs 文件中移动构造函数?
    • Sprite.cs中的那个
    • 但是在Sprite.cs中的哪里?
    • 好的,把Sprite.cs中设置加速度和速度的部分移到构造函数中,在那里设置为随机,现在每次创建一个Sprite时,都是随机的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多