Skip to content

Instantly share code, notes, and snippets.

@thaisingle
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thaisingle/1fa2c841d470bed2bc62 to your computer and use it in GitHub Desktop.
Save thaisingle/1fa2c841d470bed2bc62 to your computer and use it in GitHub Desktop.
How to create Flappy Bird (Part 2) – Add Physics to the Bird
//เราจะสร้าง Game Wolrd ของเราขึ้นมาขนาด 400 x 490
//โดยใช้ระบบ Render แบบ Auto
//และให้ Game แสดงผลใน flappyDiv คือส่วนของ <div> ที่กำหนดไว้ใน flappy.html
//จากนั้นสร้าง Game World ไว้ใน ตัวแปร game
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'flappyDiv');
//ส่วนของ logic เกมส์ที่เราเขียนแล้วเก็บไว้ในตัวแปร mainstate
var mainState = {
preload: function() {
//พื่นที่สำหรับโหลด /assets
game.stage.backgroundColor = '#71c5cf';
//โหลดรูป Bird
game.load.image('bird', 'assets/bird.png');
},
create: function() {
//พื่นที่ setup game และแสดงผล
this.bird = this.game.add.sprite(100, 245, 'bird');
//กำหนด physic ให้กับตัวแปร game world ของเราที่ชื่อ game
game.physics.startSystem(Phaser.Physics.ARCADE);
//ใช้งาน physics ให้ตัว bird
game.physics.arcade.enable(this.bird);
//กำหนดให้มีแรงโน้มถ่วงตามแนวแกน y
this.bird.body.gravity.y = 1000;
},
update: function() {
//พื่นที่ loop game 60 times ต่อวินาที
//ไว้เขียน logic
//ถ้า bird ไม่ได้อยู่ใน game world
if (this.bird.inWorld == false){
this.restartGame();
}
},
restartGame: function() {
game.state.start('main');
},
};
//ตัวแปร mainState จะถูกสร้างเป็น state ตั้งชื่อเป็น main
game.state.add('main', mainState);
//เริ่มต้น state
game.state.start('main');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment