1

クリックした場所に向かってプレーヤーを移動させます。しかし、クリックした位置に到達すると、スプライトが前後にちらつきます。スプライトがこのポイントを通過し、そこに戻り、再び通過し、その後常に「ちらつき」を作成しているためだと思います。理由はありますか?

****解決済み* ** * **** ** *更新されたコード* **

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AsteroidAvoider
{
    class Player
    {
        public Vector2 position, distance, mousePosition;
        public float speed;
        public float rotation;
        public Texture2D playerImage;
        public MouseState mouseState;

        public Player(Texture2D playerImage, Vector2 position, float speed)
        {
            this.playerImage = playerImage;
            this.position = position;
            this.speed = speed;
        }

        public void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();

            float speedForThisFrame = speed;

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                mousePosition.X = mouseState.X;
                mousePosition.Y = mouseState.Y;
            }

            if ((mousePosition - position).Length() < speed)
                speedForThisFrame = 0;

            if ((mousePosition - position).Length() > speed)
                speedForThisFrame = 2.0f;

            distance = mousePosition - position;
            distance.Normalize();

            rotation = (float)Math.Atan2(distance.Y, distance.X);

            position += distance * speedForThisFrame;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(playerImage, position, null, Color.White, rotation, new Vector2(playerImage.Width / 2, playerImage.Height / 2), 1.0f, SpriteEffects.None, 1f);
        }
    }
}
4

2 に答える 2