顯示具有 XNA 標籤的文章。 顯示所有文章
顯示具有 XNA 標籤的文章。 顯示所有文章

星期五, 7月 02, 2010

XNA 按鍵偵測

KeyboardState kbs;

kbs = Keyboard.GetState();
if (kbs.IsKeyDown(Keys.A) == true)
    move.X -= 1;
if (kbs.IsKeyDown(Keys.D) == true)
    move.X += 1;
if (kbs.IsKeyDown(Keys.S) == true)
    move.Y += 1;
if (kbs.IsKeyDown(Keys.W) == true)
    move.Y -= 1;

XNA 碰撞偵測、反彈

延續上一篇

if (squRect.Intersects(boardRectTop))
{
    move.Y *= -1; ;
}
if (squRect.Intersects(boardRectBottom))
{
    move.Y *= -1;
}
if (squRect.Intersects(boardRectLeft))
{
    move.X *= -1;
}
if (squRect.Intersects(boardRectRight))
{
    move.X *= -1;
}

可以做個class比較方便

XNA 滑鼠拉動投擲

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;