2016-07-06 10 views
0

スプライトクラスを作成して、同じスプライトの100個のコピーを画面にアニメーション表示するコードを少し書いています。私は、4輪のインターロックリングのアニメーションであるスプライトを一定の高さで描画し、画面上にドロップし、ボールのように跳ね返り、各バウンスは完全に停止するまで漸進的に少なくします。私はこの部分を完成させることができました。しかし、私は、それぞれの異なるスプライトの加速とアニメーションの速度をランダム化する方法を見つけることができません。誰かが私のコードにいくつかの提案を提供できますか?XNA 4.0で異なるプロパティを持つ複数のスプライトを描画する

ゲーム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); 
     } 
    } 
    } 
} 

答えて

0

移動コンストラクタにringSpeedとringAccelのコンストラクタとそこにそれらをランダム化。

+0

あなたの問題について具体的にお答えください。 –

+0

私は100のスプライトのそれぞれが違うアクセラレーションを持つようにしようとしています。そのため、バウンスしたときに違うスピードで動きます。また、変数 'millisecondsPerFrame'をランダム化して、スプライトにランダムなアニメーションの速度も与えようとしています。私はちょうどこれを行う方法がわかりません。あなたの提案に従えば、私は完全にはわかりません...コンストラクタを 'Sprite.cs'ファイルまたは' Game1.cs'ファイル内に移動しますか? –

+0

Sprite.csのもの –

関連する問題