PHASERJS3 应用对象池随机产生炸弹并销毁 -- JavaScript Html5 游戏开发

本文介绍了如何在PhaserJS3游戏中利用对象池(Object Pool)技术来高效地创建和销毁炸弹。通过创建一个对象组,然后使用.get方法从对象池中获取并设置炸弹位置,最后使用.killAndHide方法来销毁和隐藏炸弹,而不是直接使用.destroy。详细教程和效果预览可在提供的链接中查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

对象池 Object Pool

使用对象池 Object Pool产生炸弹,首先创建一个对象组 this.exploadGroup = this.add.group();
然后用对象组的.get重复应用对象池而不是用.create,this.exploadGroup.get(x,y,‘explode’);
最后销毁的时候不是用.destory(),而是用 this.exploadGroup.killAndHide(singleExplode);

scene.js

/// <reference path="../../libs/phaser/phaser.min.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/phaserjs3/RandomBombEffect

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值