Advertisement
Guest User

Untitled

a guest
May 24th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. //เราจะสร้าง Game Wolrd ของเราขึ้นมาขนาด 400 x 490
  2. //โดยใช้ระบบ Render แบบ Auto
  3. //และให้ Game แสดงผลใน flappyDiv คือส่วนของ <div> ที่กำหนดไว้ใน flappy.html
  4. //จากนั้นสร้าง Game World ไว้ใน ตัวแปร game
  5. var game = new Phaser.Game(400, 490, Phaser.AUTO, 'flappyDiv');
  6.  
  7. //ส่วนของ logic เกมส์ที่เราเขียนแล้วเก็บไว้ในตัวแปร mainstate
  8. var mainState = {
  9.  
  10. preload: function() {
  11. //พื่นที่สำหรับโหลด /assets
  12. game.stage.backgroundColor = '#71c5cf';
  13. //โหลดรูป Bird
  14. game.load.image('bird', 'assets/bird.png');
  15. },
  16.  
  17. create: function() {
  18. //พื่นที่ setup game และแสดงผล
  19. this.bird = this.game.add.sprite(100, 245, 'bird');
  20.  
  21. //กำหนด physic ให้กับตัวแปร game world ของเราที่ชื่อ game
  22. game.physics.startSystem(Phaser.Physics.ARCADE);
  23. //ใช้งาน physics ให้ตัว bird
  24. game.physics.arcade.enable(this.bird);
  25. //กำหนดให้มีแรงโน้มถ่วงตามแนวแกน y
  26. this.bird.body.gravity.y = 1000;
  27. },
  28.  
  29. update: function() {
  30. //พื่นที่ loop game 60 times ต่อวินาที
  31. //ไว้เขียน logic
  32. //ถ้า bird ไม่ได้อยู่ใน game world
  33. if (this.bird.inWorld == false){
  34. this.restartGame();
  35. }
  36. },
  37.  
  38. restartGame: function() {
  39. game.state.start('main');
  40. },
  41. };
  42.  
  43. //ตัวแปร mainState จะถูกสร้างเป็น state ตั้งชื่อเป็น main
  44. game.state.add('main', mainState);
  45. //เริ่มต้น state
  46. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement