Created
April 14, 2015 04:45
-
-
Save thaisingle/a7b5e4728406433ce0c1 to your computer and use it in GitHub Desktop.
How to create Flappy Bird (Part 3) – Control the bird
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//เราจะสร้าง 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; | |
//สร้าง key เมื่อมีการกดปุ่ม SPACEBAR | |
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | |
//และเมื่อปุ่มโดนกดลงให้เรียกฟังก์ชัน jump | |
spaceKey.onDown.add(this.jump, this); | |
}, | |
update: function() { | |
//พื่นที่ loop game 60 times ต่อวินาที | |
//ไว้เขียน logic | |
//ถ้า bird ไม่ได้อยู่ใน game world | |
if (this.bird.inWorld == false){ | |
this.restartGame(); | |
} | |
}, | |
jump: function() { | |
// Add a vertical velocity to the bird | |
this.bird.body.velocity.y = -350; | |
}, | |
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