惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
L
LangChain Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
D
Docker
WordPress大学
WordPress大学
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
博客园 - Franky

博客园 - 布袋

Phaser游戏框架与HTML Dom元素之间的通信交互 Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享 Phaser3跟随自定义路径移动的赛车 -- iFIERO游戏教程 用EC5/EC6自定义class的区别及用法 -- Phaser3网页游戏框架 Phaser3让超级玛丽实现轻跳、高跳及加上对应的跳跃声音 PHASER3 设置场景SCENE SLEEPING休眠和WAKE唤醒 Phaser3游戏三角学应用--一只跟随屏幕点击位置游动的鱼 Phaser3 场景Scene之间的传值 -- HTML JAVASCRIPT 网页游戏开发 Phaser3 屏幕适配iPhoneX、iPhoneXs的坑 -- JavaScript Html5 游戏开发 GameplayKit的GKStateMachine用法与实例 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域 Swift使用AVAudioPlayer来调节游戏的背景音乐大小 学好三角学(函数) — SWIFT和JAVASCRIPT游戏开发的必备技能 iFIERO.com 应用UserDefaults储存游戏分数和最高分 一步一步图文介绍SpriteKit使用TexturePacker导出的纹理集Altas SpriteKit在复制节点时留了一个巨坑给开发者,需要开发者手动把复制节点的isPaused设置为false 运用GamePlayKit的GKEntity及GKComponent 的iOS游戏开发实例 SpriteKit手机游戏摇杆JoyStick的使用 -- by iFIERO游戏开发教程 SpirteKit深度复制SKSpriteNode节点及convert转换其它Scene上的节点到当前场景坐标
Phaser3 对象池随机产生炸弹并销毁
布袋 · 2018-11-09 · via 博客园 - 布袋

效果图

效果图

对象池 Object Pool

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