Sufficiency Economy
Group Blog
 
All blogs
 
Android Programming : Game Loop

อ้างอิงจาก javacodegeeks.com

เกมทั่วไปจะมี loop เพื่อรับ input แล้วเปลี่ยนสถานะของstate เสร็จแล้ว ก็ render ภาพออกมาที่หน้าจอ
ใน android จะมี class Activity เปรียบเสมือนโต๊ะ 
ส่วน class SurfaceView เปรียบเสมือนกระดาษ สำหรับเราวาดรูป
ในขั้นตอนนี้จะมี class หลักๆ อยู่ 3 class คือ
1. DroidzActivity ซึ่ง extends จาก Activity
2. MainGamePanel ซึ่ง extends จาก class SurfaceView
3. MainThread  ซึ่ง extends จาก class Thread

สิ่งที่น่าสนใจคือ
1. เรานำclass  MainGamePanel  มาแสดงผลแทน R.Layout.main
    setContentView(new MainGamePanel(this));
2. ต้องกำหนดให้ class MainGamePanel รับ event ต่างๆ ด้วย 2 คำสั่งข้างล่างนี้ 
   getHolder().addCallback(this);
    setFocusable(true);
3. เกม loop ที่แท้จริงอยู่ใน class MainThread ซึ่งต้องใส่parameter เป็นค่า getHolder และ this = MainGamePanel
    thread = new MainThread(getHolder(), this);
4. การ ติดตามการทำงานของ โปรแกรมด้วยคำสั่ง
private static final String TAG = MainGamePanel.class.getSimpleName();
Log.d(TAG, "Surface is being destroyed");
โดยต้อง  import android.util.Log;
5. วิธีติดตามค่า คือไปที่ menu : Window -> ShowView -> LogCat
จะเกิดหน้าต่างด้านล่าง แล้วก็ search หาเหตุการณ์ของเรา




Create Date : 18 กรกฎาคม 2555
Last Update : 18 กรกฎาคม 2555 20:38:35 น. 0 comments
Counter : 1665 Pageviews.

ชื่อ :
Comment :
  *ใช้ code html ตกแต่งข้อความได้เฉพาะสมาชิก
 

wink99_th
Location :
พิษณุโลก Thailand

[Profile ทั้งหมด]

ฝากข้อความหลังไมค์
Rss Feed
Smember
ผู้ติดตามบล็อก : 8 คน [?]




New Comments
Friends' blogs
[Add wink99_th's blog to your web]
Links
 

 Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved.