主页 > canvas引擎 > Phaser游戏引擎 >
来源:未知 时间:2021-06-16 22:19 作者:小飞侠 阅读:次
[导读] 以下代码是创建2个场景,每次点击时候切换场景,每个场景都是单独随机创建一堆元素。 场景切换代码: this.scene.start(sceneA); classSceneAextendsPhaser.Scene{constructor(){super({key:sceneA});}preload()...
切换1:以下代码是创建2个场景,每次点击时候切换场景,每个场景都是单独随机创建一堆元素。 场景切换代码: this.scene.start('sceneA'); class SceneA extends Phaser.Scene { constructor () { super({ key: 'sceneA' }); } preload () { this.load.spritesheet('bobs', 'assets/sprites/bobs-by-cleathley.png', { frameWidth: 32, frameHeight: 32 }); } create () { for (var i = 0; i < 5000; i++) { // 获得2者随机 var x = Phaser.Math.Between(0, 2500); var y = Phaser.Math.Between(0, 2500); var frame = Phaser.Math.Between(0, 399); var bob = this.add.sprite(x, y, 'bobs', frame); if (i % 2) { // Hide every other sprite, it will still be on the display list though bob.setVisible(false); } } this.add.text(10, 10, 'Scene A', { font: '16px Courier', fill: '#ffffff' }); // 绑定鼠标抬起 this.input.once('pointerup', function () { //切换场景sceneB this.scene.start('sceneB'); }, this); } } class SceneB extends Phaser.Scene { constructor () { super({ key: 'sceneB' }); } create () { for (var i = 0; i < 5000; i++) { var x = Phaser.Math.Between(0, 2500); var y = Phaser.Math.Between(0, 2500); var frame = Phaser.Math.Between(0, 399); var bob = this.add.sprite(x, y, 'bobs', frame); if (i % 2) { // Hide every other sprite, it will still be on the display list though bob.setVisible(false); } } this.add.text(10, 10, 'Scene B', { font: '16px Courier', fill: '#ffffff' }); this.input.once('pointerup', function () { this.scene.start('sceneA'); }, this); } } var config = { type: Phaser.AUTO, width: 800, height: 600, backgroundColor: '#000000', parent: 'phaser-example', scene: [ SceneA, SceneB ] }; var game = new Phaser.Game(config); 切换2:此方式与1不同在于,this.scene.add('myScene', MyScene, true, { x: 400, y: 300 }); 调用此方法添加场景队列自动执行。 点击切换 class MyScene extends Phaser.Scene { preload () { this.load.image('face', 'assets/pics/bw-face.png'); } create (data) { this.face = this.add.image(data.x, data.y, 'face'); } } class BootScene extends Phaser.Scene { create () { this.add.text(0, 0, 'Click to add new Scene'); this.input.once('pointerdown', function () { this.scene.add('myScene', MyScene, true, { x: 400, y: 300 }); }, this); } } var config = { type: Phaser.AUTO, parent: 'phaser-example', width: 800, height: 600, scene: BootScene }; var game = new Phaser.Game(config); |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com