美文网首页
Phaser3 对象池随机产生炸弹并销毁 -- HTML网游教程

Phaser3 对象池随机产生炸弹并销毁 -- HTML网游教程

作者: 布袋的世界 | 来源:发表于2018-11-09 15:55 被阅读107次
效果图 对象池 Object Pool

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 -- 为游戏开发深感自豪

相关文章

  • Phaser3 对象池随机产生炸弹并销毁 -- HTML网游教程

    scene.js 效果预览地址:http://www.iFIERO.com/uploads/phaserjs3/R...

  • 微信小游戏性能优化点

    1. 对象池 多数游戏,频繁创建和销毁同类对象很常见,使用对象池已是基本操作,必须牢记并熟练使用。推荐一个应用示例...

  • effective java 第一周

    第二章:对象的产生和销毁 其围绕的主要问题是:何时,如何产生对象;何时,如何避免产生对象;如何确认对象及时销毁;以...

  • 线程池

    池的产生背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。在Ja...

  • Apache Commons-pool2要点整理

    为什么要用对象池 解决大对象的创建和销毁时的资源消耗。所以,常见的对象池有数据库连接池、线程池等 Apache C...

  • React细节知识之对象池

    React使用对象池()来管理合成事件对象的创建和销毁,在启动时React分配一个对象池,从对象池中getPo...

  • GenericObjectPool对象池实战

    通常一个对象创建/销毁的时候非常耗时,我们不会频繁的创建销毁它,而是考虑复用。复用对象的一种做法就是对象池,将创建...

  • 对象池化技术

    科普一下对象池化技术,来抄代码的直接略过就行 ; - )什么是对象池化?对象被创建后,使用完毕不是立即销毁回收对象...

  • 享元模式

    定义:是对象池的一种实现。类似于线程池,线程池可以避免不停的创建和销毁多个对象,消耗性能。提供了减少对象数量从而改...

  • 线程池

    线程池 a. 基本组成部分: 1、线程池管理器(ThreadPool):用于创建并管理线程池,包括创建线程池,销毁...

网友评论

      本文标题:Phaser3 对象池随机产生炸弹并销毁 -- HTML网游教程

      本文链接:https://www.haomeiwen.com/subject/eesoxqtx.html