Friday, August 30, 2013

Andengine - Bài 1: Cài đặt BaseGameActivity

BaseGameActivity là activity bạn phải khởi tạo khi làm việc với andengine. Giống như các Activity thông thường BaseGameActivity cũng có life cycle . Sau đây, mình sẽ giới thiệu các thành phần cũng như các phương thức mà bạn phải cài đặt.
Đầu tiên tạo class MyGameActivity extends BaseGameActivity. Lúc này bạn sẽ phải triển khai một vài phương thức, mình sẽ comment chức năng của phương thức đó.

  1. public class MyGameActivity extends BaseGameActivity{

  2. @Override
  3. public EngineOptions onCreateEngineOptions() {
  4. // Cài đặt EngineOptions với 1 camera. Ngoài ra bạn có thể tùy chọn có music,sound hay có Mult iTouch
  5. return null;
  6. }

  7. @Override
  8. public void onCreateResources(
  9. OnCreateResourcesCallback pOnCreateResourcesCallback)
  10. throws Exception {
  11. //Tạo tất cả các Resources. Load các Resourt như ảnh, font chữ, music,sound.
  12. }

  13. @Override
  14. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
  15. throws Exception {
  16. //Tạo scene và attach các thứ mà bạn vẽ vào scene
  17. }

  18. @Override
  19. public void onPopulateScene(Scene pScene,
  20. OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
  21. // Attach các thứ mà bạn thường xuyên hiển thị trên Scene.
  22. }
  23. }
Tiếp theo mình sẽ triển khai các phương thức trên :
  1. public class MyGameActivity extends BaseGameActivity{
  2.       private Camera mCamera ;//tạo camera, lưu ý camera không thể thay đổi
  3.       private int CAMERA_HEIGHT = 720;// khai báo chiều cao cho camera
  4.       private int CAMERA_WIDTH = 480;//khai báo chiều dọc cho camera
  5. @Override
  6. public EngineOptions onCreateEngineOptions() {
  7. // Cài đặt EngineOptions với 1 camera. Ngoài ra bạn có thể tùy chọn có music,sound hay có Multi Touch
  8. mCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  9. EngineOptions eni = new EngineOptions(true,
  10. ScreenOrientation.PORTRAIT_SENSOR, new FillResolutionPolicy(),
  11. this.mCamera); //set fullscreen = true , theo chiều dọc = PORTRAIT_SENSOR, full màn hình =  new FillResolutionPolicy()
  12. eni.getAudioOptions().setNeedsMusic(true);//set game  có music
  13. eni.getAudioOptions().setNeedsSound(true);//set game có Sound
  14. eni.getTouchOptions().setNeedsMultiTouch(true);
  15. if (MultiTouch.isSupported(this)) {
  16. if (MultiTouch.isSupportedDistinct(this)) {
  17. } else {
  18. }
  19. } else {
  20. Toast.makeText(
  21. this,
  22. "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.",
  23. Toast.LENGTH_LONG).show();
  24. } //set game co multi touch
  25. return eni;
  26. }

  27. @Override
  28. public void onCreateResources(
  29. OnCreateResourcesCallback pOnCreateResourcesCallback)
  30. throws Exception {
  31. //Tạo tất cả các Resources. Load các Resourt như ảnh, font chữ, music,sound.
  32.                 //Phần này mình sẽ hướng dẫn ở các bài sau. Thêm câu lệnh sau
  33.                 pOnCreateResourcesCallback.onCreateResourcesFinished();
  34. }

  35. @Override
  36. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
  37. throws Exception {
  38. //Tạo scene và attach các thứ mà bạn vẽ vào scene
  39.                 Scene currentScene= new Scene();
  40.                pOnCreateSceneCallback.onCreateSceneFinished(currentScene);
  41. }

  42. @Override
  43. public void onPopulateScene(Scene pScene,
  44. OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
  45. // Attach các thứ mà bạn thường xuyên hiển thị trên Scene.
  46.                 pOnPopulateSceneCallback..onPopulateSceneFinished();
  47. }
  48. }
Oke. Đến đây bạn đã triển khai xong BaseGameActivity. Việc còn lại bạn phải load các resourt lên và từ resourt đó tạo các hình ảnh để vẽ lên scene. Mình sẽ hướng dẫn cụ thể vào các bài sau.

15 comments:

  1. Hi! Thanks for the great information you have provided! Will come back again, Im taking your feed also, Thanks.
    facebook chat facebook inbox, autoketing.com

    ReplyDelete
  2. It’s amazing too pay a visit this site and reading the views of all mates about this article,
    while I aam also keen of getting knowledge.
    autoketing
    Shipping Bar for Shopify
    Free shipping bar

    ReplyDelete
  3. I as of late discovered numerous helpful data in your site particularly this online journal page.Among the loads of remarks on your articles. A debt of gratitude is in order for sharing
    Product discount master app, Product discount master app, autoketing

    ReplyDelete
  4. I was surprised when I read the first lines, it made me think about many things, thanks for posting it.
    currency converter box free, currency converter app download, best autoketing app

    ReplyDelete
  5. I think that thanks for the valuabe information and insights you have so provided here.
    friv10games club Games to play, jogos kizi-2018, gogy xyz Games to play

    ReplyDelete
  6. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job
    dynamons world club free games , my friend pedro club for school , pixel gun apocalypse 3 ,

    ReplyDelete
  7. Games xmas magic tiles
    Truck physics free
    Gunmaster onslaught games for kid
    Have you ever considered writing an e-book or guest authoring on other sites? I have a blog centered on the same information you discuss and would really like to have you share some stories/information. I know my subscribers would value your work. If you are even remotely interested, feel free to shoot me an email.

    ReplyDelete
  8. . What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info
    mr dubstep free , game moto x3m 4 winter , santa claus jump unblocked games ,

    ReplyDelete
  9. I am crazy about this blog. This website basically beautiful points and things right.Much obliged for setting aside an ideal opportunity to examine this, I feel firmly about it and love adapting more on this point. Kick The Buddy game for kids free game Def Island Papa's Scooperia free game to play

    ReplyDelete
  10. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors.
    bob the robber 4 japan online , fireboy and watergirl 4 best online games , mario banzai best online games ,

    ReplyDelete


  11. food games
    basketball games 2019
    soccer free games
    I used to happily find this web-site. I anticipated that would an obligation of appreciation is all together for your possibility for this remarkable learn!! I unequivocally getting a charge out of every single bit of it and I've you bookmarked to take a gander at new stuff you blog. Thanks

    ReplyDelete
  12. Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next. 2player game online
    io jogos gratis
    free online juegos games

    ReplyDelete

  13. Barbie Beauty Bath games
    Golf Park games online
    play Football Heads: 2013-14 Serie A games
    I really thank you for the valuable info on this great subject and look forward to more great posts

    ReplyDelete