經過上篇文章的學習,咱們已經能經過鍵盤來控制小鳥,包括小鳥碰撞是否超出窗口,以及管道循環,還有分數面板的檢測,接下來咱們繼續往下進行。javascript
update: function() { //若是小鳥超出窗口,重置遊戲 if (this.bird.inWorld == false) this.restart_game(); // 碰撞與接觸感應物理引擎,小鳥跟管道 ,重置遊戲 this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this); },
這裏面的overlap是指,僅僅檢測是否碰撞,而且分別給定了三個對象。java
在這個遊戲裏面咱們是用鍵盤事件控制小鳥,因此咱們要檢測每次按空格時,調用的函數,從而得到小鳥的碰撞感應,git
jump: function() { // 檢測小鳥碰撞 獲取降低高度 告訴你撞管子了 this.bird.body.velocity.y = -350; },
就想描述同樣,當小鳥撞擊的時候,高度直接爲-350,往下來,若是撞擊了=game over,這個是必然事件。哈哈,因此咱們往下來看看dom
// 遊戲重新開始, restart_game: function() { //移除對象 this.game.time.events.remove(this.timer); //重新開始遊戲 this.game.state.start('main'); },
這裏面的移除對象,移除的是上面的時鐘對象(循環制造管道),就是不讓他繼續遊戲了!函數
以後重新開始遊戲,學習
從如今開始咱們將給管道進行添加,PS(剛剛一直在循環管道,可是管道的具體位置並無提到)this
// 添加管道的循環 add_one_pipe: function(x, y) { // 獲取第一組管道 var pipe = this.pipes.getFirstDead(); // 設置管道的X Y位置 pipe.reset(x, y); // 添加管道的X軸速度精靈 pipe.body.velocity.x = -200; // 設置其他管道的不可見 pipe.outOfBoundsKill = true; },
上面的代碼實說,我第一組管道的製做方法,以及管道速度,兄弟管道(其他的管道)不能夠看到spa
// 添加隨機上下管道 add_row_of_pipes: function() { var hole = Math.floor(Math.random()*5)+1; // for (var i = 0; i < 8; i++) if (i != hole && i != hole +1) this.add_one_pipe(400, i*60+10); this.score += 1; //分數更新統計 this.label_score.content = this.score; //分數結果,結束 }, }; //添加遊戲畫布,遊戲開始 game.state.add('main', game_state.main); game.state.start('main');
這是本文章最後的結尾,然而這僅僅只是Phaser.js的一小部分,想更多的瞭解 可讓咱們一塊兒溝通,學習進步.net
最後提供給你們這個遊戲下載的地址 : http://git.oschina.net/8946a/bird_-jumping rest