您好,登錄后才能下訂單哦!
小編給大家分享一下實現跑酷游戲的方法是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
采用的物理引擎是:Phaser.js
官網地址:http://phaser.io/
在這里對此引擎不做過多介紹(因為我也是小白,嘿嘿)
效果展示:
1.創建游戲舞臺
var config = { type: Phaser.AUTO, width: 800, height: 400, physics: { default: 'arcade', arcade: { gravity: { y: 300 }, debug: false } }, scene: { preload: preload, create: create, update: update } }; var game = new Phaser.Game(config); // 創建游戲
2.載入資源
function preload() { this.load.image('sky', 'assets/sky.png'); this.load.image('ground', 'assets/platform.png'); 5 6 this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 }); }
3.將資源創建到舞臺上
var distanceText; // 路程文本 var distance = 0; // 路程 var platforms; // 地面 var player; // 玩家 var enemy; // 敵人 var enemys; // 敵人們 var enemyTimer; // 敵人計時器 var distanceTimer; // 路程計時器 function create() { // 添加畫布背景 this.add.image(400, 200, 'sky'); // 添加分數文本 distanceText = this.add.text(16, 16, 'Distance: 0m', { fontSize: '20px', fill: '#000' }); // 添加地面 platforms = this.physics.add.staticGroup(); platforms.create(400, 400, 'ground').setScale(3).refreshBody(); // 添加玩家(精靈) player = this.physics.add.sprite(100, 300, 'dude'); player.setBounce(0); // 設置阻力 player.setCollideWorldBounds(true); // 禁止玩家走出世界 // 敵人 enemys = this.physics.add.group(); enemys.children.iterate(function (child) { child.setCollideWorldBounds(false); }); // 動態創建敵人 enemyTimer = setInterval(function () { enemy = enemys.create(1000, 300, 'dude'); enemy.setTint(getColor()); enemy.anims.play('left', true); enemy.setVelocityX(Phaser.Math.Between(-300, -100)); }, Phaser.Math.Between(4000, 8000)) distanceTimer = setInterval(function () { distance += 1; distanceText.setText('Distance: ' + distance + 'm'); }, 1000) this.physics.add.collider(player, platforms); //玩家在地面上 this.physics.add.collider(enemys, platforms); //敵人在地面上 this.physics.add.collider(player, enemys, hitBomb, null, this); }
4.在創建場景過程中寫鍵盤監聽事件
var cursors; // 按鍵 // 事件 this.anims.create({ key: 'left', frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }), frameRate: 10, repeat: -1 }); this.anims.create({ key: 'right', frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }), frameRate: 10, repeat: -1 }); this.anims.create({ key: 'turn', frames: [{ key: 'dude', frame: 4 }], frameRate: 20 }); cursors = this.input.keyboard.createCursorKeys();
5.寫碰撞函數(當玩家與敵人碰撞的結果)
var gameOver = false; // 游戲結束 function hitBomb(player, enemys) { this.physics.pause(); clearInterval(enemyTimer); clearInterval(distanceTimer); player.setTint(0xff0000); gameOver = true; alert('游戲結束,您跑了' + distance + 'm'); }
6.在update函數中寫時間的執行(須注意的是此函數每一幀都在執行,1幀≠1秒)
function update() { if (cursors.up.isDown && player.body.touching.down) { player.setVelocityY(-220); } else { player.anims.play('right', true); } if (gameOver) { player.setVelocityX(0); player.anims.play('turn'); return; } }
這里我給敵人上了顏色的,隨機16進制顏色
function getColor() { var color = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"].sort(function(){ return Math.random() - 0.5 }).join("").substr(0,6); return "0x" + color; }
整個源碼奉上(建議去github上自己clone):
var config = { type: Phaser.AUTO, width: 800, height: 400, physics: { default: 'arcade', arcade: { gravity: { y: 300 }, debug: false } }, scene: { preload: preload, create: create, update: update } }; var game = new Phaser.Game(config); // 創建游戲 var distanceText; // 路程文本 var distance = 0; // 路程 var platforms; // 地面 var player; // 玩家 var enemy; // 敵人 var enemys; // 敵人們 var gameOver = false; // 游戲結束 var enemyTimer; // 敵人計時器 var distanceTimer; // 路程計時器 var cursors; // 按鍵 // 載入資源 function preload() { this.load.image('sky', 'assets/sky.png'); this.load.image('ground', 'assets/platform.png'); 39 40 this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 }); } // 將資源展示到畫布創建資源 function create() { // 添加畫布背景 this.add.image(400, 200, 'sky'); // 添加分數文本 distanceText = this.add.text(16, 16, 'Distance: 0m', { fontSize: '20px', fill: '#000' }); // 添加地面 platforms = this.physics.add.staticGroup(); platforms.create(400, 400, 'ground').setScale(3).refreshBody(); // 添加玩家(精靈) player = this.physics.add.sprite(100, 300, 'dude'); player.setBounce(0); // 設置阻力 player.setCollideWorldBounds(true); // 禁止玩家走出世界 // 敵人 enemys = this.physics.add.group(); enemys.children.iterate(function (child) { child.setCollideWorldBounds(false); }); // 事件 this.anims.create({ key: 'left', frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }), frameRate: 10, repeat: -1 }); this.anims.create({ key: 'right', frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }), frameRate: 10, repeat: -1 }); this.anims.create({ key: 'turn', frames: [{ key: 'dude', frame: 4 }], frameRate: 20 }); cursors = this.input.keyboard.createCursorKeys(); // 動態創建敵人 enemyTimer = setInterval(function () { enemy = enemys.create(1000, 300, 'dude'); enemy.setTint(getColor()); enemy.anims.play('left', true); enemy.setVelocityX(Phaser.Math.Between(-300, -100)); }, Phaser.Math.Between(4000, 8000)) distanceTimer = setInterval(function () { distance += 1; distanceText.setText('Distance: ' + distance + 'm'); }, 1000) this.physics.add.collider(player, platforms); //玩家在地面上 this.physics.add.collider(enemys, platforms); this.physics.add.collider(player, enemys, hitBomb, null, this); } // 一直執行 function update() { if (cursors.up.isDown && player.body.touching.down) { player.setVelocityY(-220); } else { player.anims.play('right', true); } if (gameOver) { player.setVelocityX(0); player.anims.play('turn'); return; } } function hitBomb(player, enemys) { this.physics.pause(); clearInterval(enemyTimer); clearInterval(distanceTimer); player.setTint(0xff0000); gameOver = true; alert('游戲結束,您跑了' + distance + 'm'); } function getColor() { var color = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"].sort(function(){ return Math.random() - 0.5 }).join("").substr(0,6); return "0x" + color; }
以上是實現跑酷游戲的方法是什么的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。