0

ここで、タイマーを使用して、ユーザー入力なしでこのアニメーション化された長方形を水平に移動したいですか? アニメーション化された長方形は左右に移動する必要があります (始点から終点まで、パス全体 = 315 ピクセル)。つまり、ポイント 1 (左から開始) とポイント 2 (右から終了) の間は 315 ピクセルです。これが私のコードです: 変数:

        //Sprite Texture
        Texture2D texture;
        //A Timer variable
        float timer = 0f;
        //The interval (300 milliseconds)
        float interval = 300f;
        //Current frame holder (start at 1)
        int currentFrame = 1;
        //Width of a single sprite image, not the whole Sprite Sheet
        int spriteWidth = 32;
        //Height of a single sprite image, not the whole Sprite Sheet
        int spriteHeight = 32;
        //A rectangle to store which 'frame' is currently being shown
        Rectangle sourceRect;
        //The centre of the current 'frame'
        Vector2 origin;

次に、LoadContent()メソッドで:

       //Texture load
       texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");

Update()メソッド:

    //Increase the timer by the number of milliseconds since update was last called
    timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
    //Check the timer is more than the chosen interval
    if (timer > interval)
    {
        //Show the next frame
        currentFrame++;
        //Reset the timer
        timer = 0f;
    }
    //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
    currentFrame = currentFrame % 2;
    sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
    origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);

そして最後に、Draw()メソッド:

    //Texture Draw
    spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);

だから私は移動したいsourceRect。Ir は左右にループする必要があります。

4

1 に答える 1