2016-07-06 3 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의 하나 –

관련 문제