摘要:一首先當(dāng)然得有至少有二個(gè)場(chǎng)景二從場(chǎng)景傳值到場(chǎng)景二種方法通過(guò)事件從通過(guò)時(shí)傳值到時(shí)有個(gè)需要特別注的事項(xiàng)就是,得把的設(shè)為否則因?yàn)檫€未激活,是監(jiān)聽(tīng)不到事件的通過(guò)場(chǎng)景啟動(dòng)具體詳見(jiàn)代碼在整個(gè)工程中只會(huì)執(zhí)行一次每次調(diào)用場(chǎng)景會(huì)執(zhí)行一次從事件傳
一、首先當(dāng)然得有至少有二個(gè)場(chǎng)景sceneA.js,sceneB.js
二、從場(chǎng)景A傳值到場(chǎng)景B二種方法
1)通過(guò)事件this.events.emit("event key",{objKey:objValue});
從sceneA通過(guò) ths.events.emit時(shí)傳值到sceneB時(shí)有個(gè)需要特別注的事項(xiàng)就是,得把sceneB的 active設(shè)為 ture,否則因?yàn)?sceneB還未激活,是監(jiān)聽(tīng)不到 events.on事件的!!!
2)通過(guò)場(chǎng)景啟動(dòng)this.scene.start("gameB key",{objKey:objValue});
具體詳見(jiàn)代碼:
HTML
iFIERO Games Tutorial ©Copyrigths By www.iFIERO.com
sceneA.js
"use strict"; var SceneA = new Phaser.Class({ Extends: Phaser.Scene, // 在整個(gè)工程中只會(huì)執(zhí)行一次 initialize: function BootScene() { Phaser.Scene.call(this, { key: "sceneA", active: false // listening resize event; }); }, // 每次調(diào)用場(chǎng)景SceneA會(huì)執(zhí)行一次; init: function () { }, preload:function(){ }, create:function(){ // 1. 從SCENEA emit gameCountDown事件,傳送 {countDown:10} 對(duì)象到場(chǎng)景B sceneB this.events.emit("gameCountDown",{countDown:10}); //* 事件KEY=>gameCountDown // 2.start方法傳送 this.scene.start("sceneB",{countDown:10}) //* 場(chǎng)景KEY=> sceneB }, });
sceneB.js
"use strict"; var SceneB = new Phaser.Class({ Extends: Phaser.Scene, initialize: function BootScene() { Phaser.Scene.call(this, { key: "sceneB", active: true // listening resize event; }); }, init: function (data) { //方法1. 引入sceneA 在初始化的時(shí)候就可以獲得場(chǎng)景Scene傳遞的值; this.sceneA = this.scene.get("sceneA"); // sceneA"s key console.log("get data from sceneA:",data.countDown); }, preload:function(){ }, create:function(){ // 方法2.監(jiān)聽(tīng)scene的事件 gameCountDown this.sceneA.events.on("gameCountDown",function(data){ console.log(data.countDown); }); }, });
gameconfig.js
var game; //* setDepth for every object; var gameConfig = { depth:{ player:1, } } // once the window loads... window.onload = function () { // 接收 websocket; // config of the game; var config = { type: Phaser.AUTO, parent: "game", width: 640, // don"t window.innerWidth height: 512, physics: { default: "arcade", arcade: { gravity: { y: 0 }, debug: false, } }, //*** scenes used by the game // scene: [BootScene,PlayGameScene,UIScene] } game = new Phaser.Game(config); game.scene.add("sceneA", SceneA); //*** key,class */ game.scene.add("sceneB", SceneB); window.focus(); resize(); window.addEventListener("resize", resize, false); } function resize() { var canvas = document.querySelector("canvas"); var windowWidth = window.innerWidth; var windowHeight = window.innerHeight; var windowRatio = windowWidth / windowHeight; var gameRatio = game.config.width / game.config.height; if (windowRatio < gameRatio) { canvas.style.width = windowWidth + "px"; canvas.style.height = (windowWidth / gameRatio) + "px"; } else { canvas.style.width = (windowHeight * gameRatio) + "px"; canvas.style.height = windowHeight + "px"; } }
結(jié)語(yǔ): 用Phaserjs3 JavaScript框架 來(lái)開(kāi)發(fā)HTML網(wǎng)頁(yè)游戲,雖不復(fù)雜,但有道是『紙上得來(lái)終覺(jué)淺,絕知此事要躬行』,代碼還是得親自多碼才行噢!
更多游戲教學(xué):http://www.iFIERO.com -- 為游戲開(kāi)發(fā)深感自豪
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/99123.html
摘要:一首先當(dāng)然得有至少有二個(gè)場(chǎng)景二從場(chǎng)景傳值到場(chǎng)景二種方法通過(guò)事件從通過(guò)時(shí)傳值到時(shí)有個(gè)需要特別注的事項(xiàng)就是,得把的設(shè)為否則因?yàn)檫€未激活,是監(jiān)聽(tīng)不到事件的通過(guò)場(chǎng)景啟動(dòng)具體詳見(jiàn)代碼在整個(gè)工程中只會(huì)執(zhí)行一次每次調(diào)用場(chǎng)景會(huì)執(zhí)行一次從事件傳 showImg(https://segmentfault.com/img/remote/1460000016953682); 一、首先當(dāng)然得有至少有二個(gè)場(chǎng)景sc...
摘要:前言作為一款流行的游戲動(dòng)畫(huà)框架受到很多開(kāi)發(fā)者的青睞最近筆者在逛意大利開(kāi)發(fā)者論壇的時(shí)候發(fā)現(xiàn)了這款小游戲所以就照著說(shuō)明做了一下在這里記錄下來(lái)開(kāi)發(fā)準(zhǔn)備插件腳本飛刀和靶子的圖像或者這個(gè)項(xiàng)目里面有的腳本和需要的圖像文件開(kāi)始制作搭建基本的項(xiàng)目創(chuàng)建一個(gè) 前言 phaser作為一款流行的游戲/動(dòng)畫(huà)框架,受到很多web開(kāi)發(fā)者的青睞,最近筆者在逛意大利開(kāi)發(fā)者:emanueleferonato論壇的時(shí)候發(fā)現(xiàn)...
摘要:自定義自定義完整代碼更多游戲教學(xué)為游戲開(kāi)發(fā)深感自豪 showImg(https://segmentfault.com/img/remote/1460000017262663); EC6 自定義class class Brain extends Phaser.GameObjects.Sprite { constructor (scene, x, y) { ...
摘要:自定義自定義完整代碼更多游戲教學(xué)為游戲開(kāi)發(fā)深感自豪 showImg(https://segmentfault.com/img/remote/1460000017262663); EC6 自定義class class Brain extends Phaser.GameObjects.Sprite { constructor (scene, x, y) { ...
閱讀 1589·2023-04-26 01:54
閱讀 1621·2021-09-30 09:55
閱讀 2645·2021-09-22 16:05
閱讀 1856·2021-07-25 21:37
閱讀 2620·2019-08-29 18:45
閱讀 1886·2019-08-29 16:44
閱讀 1882·2019-08-29 12:34
閱讀 1346·2019-08-23 14:02