ตรวจสอบ mouse กดแช่
posted on 27 Jun 2008 10:58 by xnanoob in Tutorials
เรียนเขียนเกมส์กับ XNANOOB ด้วย xna framework
ดีค่ะ มีจดหมายจากทางบ้านเข้ามาสอบถามเรื่องการควบคุม mouse ทำอย่างไรให้ตรวจสอบการคลิ๊กได้
แต่มีข้อแม้ว่ากดแช่ไม่ได้ (ตัวอย่างของ keyboard ซึ่งใช้ท่าเดียวกัน)
เนื่องจากในส่วนของ update ใน xna frame work จะมีการทำงานที่รวดเร็วมาก ดังนั้นใน 1 วินาที
หรือจังหวะที่เรากดจะมีการรับคำสั่งมากเกินไป ดังนั้นจึงต้องมีการตรวจสอบสถานะก่อนให้ทำงานหรือตาม logic ของคุณ
สร้าง object MouseState ขึ้นมาก่อน
Code:
MouseState mouseStateCurrent, mouseStatePrevious;
ในส่วนของ Update method ทำการตรวจสอบสถานะในขณะกด ป้องกันการกดแช่
จากสถานกดกดล่าสุดจะกลายเป็นสถานะก่อนหน้านี้
และตรวจสอบว่าสถานะล่าสุดกับสถานะก่อนหน้านี้ต้องเท่ากันคือมีการปล่อยมือนั้นเองเพื่อล้างค่า
Code:
protected override void Update(GameTime gameTime)
{
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton = ButtonState.Pressed &&
mouseStatePrevious.LeftButton = ButtonState.Released)
{
// Do your mouse state logic here
}
mouseStatePrevious = mouseStateCurrent;
base.Update(gameTime);
}
{
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton = ButtonState.Pressed &&
mouseStatePrevious.LeftButton = ButtonState.Released)
{
// Do your mouse state logic here
}
mouseStatePrevious = mouseStateCurrent;
base.Update(gameTime);
}
งุงิ จบแระค่า บายค่ะ
ปล.ดูแบบสีได้ที่ http://www.asiamediasoft.net/forum/index.php?topic=28.0
อบรม เรียน สอน Solidworks โดย อ.ศุภชัย สนใจติดต่อ 02-9373790 หรือ 080-0505-807,081-1716156
Tags: mousestate, xna, xnanoob0 Comments







