UPDATA:
MouseState preMouseState, mouseState;
bool flag = false;
Vector2 move;
mouseState = Mouse.GetState();
Rectangle squRect = new Rectangle((int)position.X, (int)position.Y, square.Width, square.Height);
Rectangle mouseRect = new Rectangle(mouseState.X, mouseState.Y, 0, 0);
if (squRect.Intersects(mouseRect) && mouseState.LeftButton == ButtonState.Pressed && flag == false) //squRect.Intersects(mouseRect) : //滑鼠點擊的地方是否在方框內
{
preMouseState = mouseState;
flag = true;
}
if (mouseState.LeftButton == ButtonState.Released && flag == true) //已放開且上個狀態是按下
{
move.X = (mouseState.X - preMouseState.X) * 0.3F;
move.Y = (mouseState.Y - preMouseState.Y) * 0.3F;
flag = false;
}
//move是加速度, position是square的座標
position.X += move.X;
position.Y += move.Y;
//漸變阻尼
if (position.Y >= height - square.Height)
moveX *= 0.95;
else
moveX *= 0.995; //碰到地板阻尼較大
moveY *= 0.98;
沒有留言:
張貼留言