当前位置: 首页 > Web前端 > HTML5

Phaserjs3对象池随机生成炸弹并销毁--Html网页游戏开发

时间:2023-04-05 17:28:45 HTML5

scene.js///'usestrict';varBootScene=newPhaser.Class({Extends:Phaser.Scene,初始化:functionBootScene(){Phaser.Scene.call(this,{key:'Boot',active:true//监听调整大小事件;});},init:function(){console.log('initbootscene');this.particles={};},preload:function(){this.load.image('sky','assets/space.jpg');this.load.spritesheet('explode','assets/explode.png',{frameWidth:128,frameHeight:128})},create:function(){//this.sound.play('hitbomb');this.background=this.add.sprite(0,0,'sky').setOrigin(0,0);this.createExplode();},update:function(){//console.log('bootsceneupdate');},createExplode:function(){//*单个图像;//this.add.sprite(200,200,'explode',10);this.anims.create({键:'explodeAnimation',帧:this.anims.generateFrameNumbers('explode',{开始:0,结束:16,第一:0}),帧率:25,重复:0});//*池组this.exploadGroup=this.add.group();this.time.addEvent({delay:2500,//repeat:-1,callbackScope:this,callback:this.spawnRandomExplode});},spawnRandomExplode:function(){letx=Phaser.Math.Between(10,this.sys.game.config.width);让y=Phaser.Math.Between(10,this.sys.game.config.height);//this.add.sprite(x,y,'explode').setScale(0.7).play('explodeAnimation');让singleExplode=this.exploadGroup.get(x,y,'explode');//*进入池而不是创建singleExplode.setScale(0.7).play('explodeAnimation');//*播放动画;singleExplode.setActive(true);singleExplode.setVisible(true);//爆炸lifetimethis.explosionTimeEvent=this.time.addEvent({delay:600,//delay5sdeletethis.bomb;repeat:0,callbackScope:this,callback:function(){this.exploadGroup.killAndHide(singleExplode);//*this.explosionTimeEvent.remove(false);}});},});效果预览地址:http://www.iFIERO.com/uploads...更多游戏教学:http://www.iFIERO.com——以游戏开发为荣