资讯专栏INFORMATION COLUMN

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

xushaojieaaa / 1047人阅读

scene.js

/// 
 
"use strict";
var BootScene = new Phaser.Class({
    Extends: Phaser.Scene,

    initialize: function BootScene() {

        Phaser.Scene.call(this, {
            key: "Boot",
            active: true // listening resize event;
        });
   
    },
    init: function () {
        console.log("init bootscene");
        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("bootscene update");
    },
    
    createExplode:function(){
        //* single image;
        // this.add.sprite(200,200,"explode",10);
        this.anims.create({
            key: "explodeAnimation",
            frames: this.anims.generateFrameNumbers("explode", { start: 0, end: 16, first: 0 }),
            frameRate: 25,
            repeat: 0
        });
        //* pool group
        this.exploadGroup = this.add.group();
        
        this.time.addEvent({
            delay: 2500, //  
            repeat: -1,
            callbackScope: this,
            callback: this.spawnRandomExplode
        });
    },
    spawnRandomExplode:function(){
        let x = Phaser.Math.Between(10, this.sys.game.config.width);
        let y = Phaser.Math.Between(10,this.sys.game.config.height);
       // this.add.sprite(x,y,"explode").setScale(0.7).play("explodeAnimation");
        let singleExplode = this.exploadGroup.get(x,y,"explode"); //* get to pool instead of create
        singleExplode.setScale(0.7).play("explodeAnimation"); //* play animation;
        singleExplode.setActive(true);
        singleExplode.setVisible(true);

        // explosion lifespan
        this.explosionTimeEvent = this.time.addEvent({
            delay: 600, // delay 5s 删除 this.bomb;
            repeat: 0,
            callbackScope: this,
            callback:function(){
                this.exploadGroup.killAndHide(singleExplode);
              //*  this.explosionTimeEvent.remove(false);
            }
        });
            
        
    }, 

});

效果预览地址:http://www.iFIERO.com/uploads...

更多游戏教学:http://www.iFIERO.com -- 为游戏开发深感自豪

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/99154.html

相关文章

  • Phaserjs3 对象随机产生炸弹销毁 -- Html网页游戏开发

    showImg(https://segmentfault.com/img/remote/1460000016964470); showImg(https://segmentfault.com/img/remote/1460000016964471); scene.js /// use strict; var BootScene = new Phaser.Class({ Extends...

    wind3110991 评论0 收藏0
  • 无聊吗,写个【飞机大战】来玩吧

    摘要:今天杭州又是大雨,被淋了个落汤鸡,都怪我家大狼狗非要骑电动车,我昨天吐槽要买的帅气的雨衣还没有买不过大雨和飞机大战小游戏更配哦。微信早已正式发布微信内置飞机大战游戏,目前该游戏已经下线。此时,界面中会显示此次玩家的飞机大战分数。showImg(https://user-gold-cdn.xitu.io/2019/5/15/16ab9377884b99f7); 今天杭州又是大雨,被淋了个落汤鸡...

    李涛 评论0 收藏0
  • 无聊吗,写个【飞机大战】来玩吧

    摘要:今天杭州又是大雨,被淋了个落汤鸡,都怪我家大狼狗非要骑电动车,我昨天吐槽要买的帅气的雨衣还没有买不过大雨和飞机大战小游戏更配哦。微信早已正式发布微信内置飞机大战游戏,目前该游戏已经下线。此时,界面中会显示此次玩家的飞机大战分数。 今天杭州又是大雨,被淋了个落汤鸡,都怪我家大狼狗非要骑电动车,我昨天吐槽要买的帅气的雨衣还没有买,不过大雨和飞机大战小游戏更配哦。 这篇文章来自我司的王老吉同...

    MSchumi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<