当前位置:   article > 正文

phaser.js,主要实现了移动底部篮子,接取下落物体_js实现接掉落的物体

js实现接掉落的物体

1.我将phaser分为3个文件,资源加载BootScene.js,逻辑处理PlayScene.js,整合game.js加粗样式

1.1资源加载BootScene.js

import { Scene } from 'phaser';

// 本地
import bgMusic from '@src/assets/music/bgMusic.mp3';
import bomb from "@src/assets/receiveEuropeanGas/bomb.png"
import european_gas from "@src/assets/receiveEuropeanGas/european_gas.png"
import purse from "@src/assets/receiveEuropeanGas/purse.png"
import blessing from "@src/assets/receiveEuropeanGas/blessing.png"
import gameBg from "@src/assets/receiveEuropeanGas/background.png"
import time01 from '@src/assets/receiveEuropeanGas/first.png';
import time02 from '@src/assets/receiveEuropeanGas/second.png';
import time03 from '@src/assets/receiveEuropeanGas/third.png';
import animation1 from '@src/assets/receiveEuropeanGas/award-animation.png';
import animation2 from '@src/assets/receiveEuropeanGas/bomb-animation.png';
import back from '@src/assets/receiveEuropeanGas/back.png';
import banner from '@src/assets/receiveEuropeanGas/banner.png';
import closeMusic from '@src/assets/receiveEuropeanGas/closeMusic.png';
import openMusic from '@src/assets/receiveEuropeanGas/openMusic.png';

export default class BootScene extends Scene {
    constructor() {
        super({
            key: 'BootScene'
        });
    }

    preload() {
        let wThat = window.$vueThat;
        this.load.on('progress', (value) => {
            wThat.progressNum = parseInt(value * 100);
            if (value >= 1) {
                wThat.progressNum = parseInt(value * 100);
            }
        });

         //本地
         this.load.audio('bgMusic', bgMusic);
         this.load.image('gameBg', gameBg);
         this.load.image('time01', time01);
         this.load.image('time02', time02);
         this.load.image('time03', time03);
         this.load.image('purse', purse);
         this.load.image('bomb', bomb);
         this.load.image('european_gas', european_gas);
         this.load.image('blessing', blessing);
         this.load.image('animation1', animation1);
         this.load.image('animation2', animation2);
         this.load.image('back', back);
         this.load.image('banner', banner);
         this.load.image('closeMusic', closeMusic);
         this.load.image('openMusic', openMusic);
    }

    create() {
        this.scene.start('PlayScene');
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1.2逻辑处理PlayScene.js

// import * as Types from '@src/store/types';
import {
    Scene
} from 'phaser';

const Store = window.$vueThat.$store;
const Router = window.$vueThat.$router;

const State = Store.state;
const Common = State.common;
const Commit = Store.commit;
const Fetch = Store.Fetch;

export default class PlayScene extends Scene {
    constructor() {
        super({
            key: 'PlayScene'
        });
        this.wWidth = window.innerWidth;
        this.wHeight = window.innerHeight;
        this.actualHeight = (this.wHeight * 1080) / this.wWidth;
        this.timedEvent = '';
        this.Store = window.$vueThat.$store;

        this.isCountDown = false; //倒计时动画开关
        this.isStartGame = false; //游戏开始开关
        this.isPause = false; //游戏暂停开关
        this.isGamePop = false; //游戏弹窗状态
        this.isDropGift = true; //掉礼包开关
        this.isClickBack = false; //返回开关
        this.isDisplayGift = false; //显示礼品
        this.isEnterGame = false;
        this.isMusic = false; //音乐开关
        this.count = 100000; //游戏时间,默认100000,一般无法超过
        this.bombs = null;
        this.silveGifts = null;
        this.generatorTimerLeft = null;
        this.generatorTimerMiddie = null
        this.generatorTimerRight = null;
        this.animation2 = null
        this.animation1 = null

        this.aniArr = [];
        this.mask = null
        this.isHaveAward = false
        this.closeMusic = null
        this.openMusic = null
        this.isClickMusic = false
        this.openMusicBag = null

    }

    between(min, max) {
        return Math.floor(Math.random() * (max + 1 - min) + min);
    }

    preload() {
    }

    create() {
        window.$phaserThis = this;
        console.log("重新开始游戏", )
        //图片层级按顺序变大
        this.gameBg = this.add
            .image(0, 0, 'gameBg')
            .setOrigin(0, 0)
            .setScale(1);
        this.back = this.physics.add.sprite(944, 580, 'back').setScale(1)//为了层级大于banner
        this.openMusicBag = this.add
            .image(63, 380, 'openMusic')
            .setOrigin(0, 0)
            .setScale(1);
        this.banner = this.add
            .image(95, 264, 'banner')
            .setOrigin(0, 0)
            .setScale(1);
        this.banner.setDepth(5)
        //顶部栏

        this.purse = this.physics.add.sprite(540, this.actualHeight - 401, 'purse').setScale(0.797)
        this.purse.setCollideWorldBounds(true);
        this.purse.setInteractive();
        this.input.setDraggable(this.purse);

        this.back.setCollideWorldBounds(true);
        this.back.setInteractive();
        this.input.setDraggable(this.back);


        //礼包
        this.bombs = this.physics.add.group({
            key: 'bomb',
        });
        this.silveGifts = this.physics.add.group({
            key: 'european_gas',
            key: 'blessing',
        });
        //将掉落礼包分为左中右三部分下落
        this.generatorTimerLeft = this.createCandies(100, 440)
        this.generatorTimerMiddie = this.createCandies(440, 640)
        this.generatorTimerRight = this.createCandies(640, 940)
    } // create

    update() {
        this.isClickBack = Common.isClickBack;
        this.isEnterGame = Common.isEnterGame
        //1. 触发了返回按钮, 暂停
        if (this.isClickBack && this.isEnterGame) {
            if (this.silveGifts) {
                this.silveGifts.children.entries.forEach((item) => {
                    item.destroy();
                });
            }
            if (this.bombs) {
                this.bombs.children.entries.forEach((item) => {
                    item.destroy();
                });
            }
            // console.log('1. 点击返回');
            this.aniArr = [];
            this.purse.removeListener('drag');
            this.isPause = true;
            this.sound.pauseAll()
            if (this.isClickMusic) {
                this.closeMusic.destroy()
            }

            this.isDropGift = false;
            this.isMusic = true
            this.isHaveAward = false
            this.physics.pause();
            this.generatorTimerLeft.paused = true;
            this.generatorTimerMiddie.paused = true;
            this.generatorTimerRight.paused = true;
            this.isGamePop = true;
            return;
        }
        //2. 弹窗关闭
        if (Common.gamePopType === 'close' && this.isGamePop) {
            console.log('2. 弹窗');
            this.isCountDown = false; //倒计时动画开关
            this.isStartGame = false; //游戏开始开关
            this.isPause = false; //游戏暂停开关
            this.isMusic = false //音乐开关
            this.isGamePop = false; //游戏弹窗状态
            //  this.isDropGift = true; //掉礼包开关打开
            this.physics.resume(); //游戏继续
            if (this.animation2) {
                this.animation2.destroy()
            }
            if (this.animation1) {
                this.animation1.destroy()
            }
            if (this.purse) {
                this.purse.destroy()
            }
            //重新使接去袋子居中
            this.purse = this.physics.add.sprite(540, this.actualHeight - 401, 'purse').setScale(0.797)
            this.purse.setCollideWorldBounds(true);
            this.purse.setInteractive();
            this.input.setDraggable(this.purse);
            this.generatorTimerLeft.paused = false;
            this.generatorTimerMiddie.paused = false;
            this.generatorTimerRight.paused = false;
            if (window.$phaserThis.count !== 100000) window.$phaserThis.count = 100000;
        }

        //3. 没有倒计时,开启倒计时
        if (!this.isCountDown && window.$vueThat.loaded) {
            console.log('3.倒计时');
            this.purse.removeListener('drag');
            this.aniArr = ['03', '02', '01'];
            this.isCountDown = true;
            this.countDown();
        }

        //4. 游戏为开始
        if (!this.isStartGame) {
            console.log('4游戏开始')
            return;
        }

        //5. 倒计时为0 且 未暂停
        if (+this.timedEvent.repeatCount === 0 && !this.isPause) {
            if (this.silveGifts) {
                this.silveGifts.children.entries.forEach((item) => {
                    item.destroy();
                });
            }
            if (this.bombs) {
                this.bombs.children.entries.forEach((item) => {
                    item.destroy();
                });
            }
            console.log('5. 暂停');
            //时间不足,游戏暂停
            this.isPause = true;
            this.physics.pause();
            //根据不同的状况不同的弹窗:
            this.isGamePop = true;
            this.generatorTimerLeft.paused = true;
            this.generatorTimerMiddie.paused = true;
            this.generatorTimerRight.paused = true;
            this.isStartGame = true;
            return;
        }

        //6.游戏开始
        if (this.isStartGame) {
            this.dropGift();
        }
    }
    checkOverlap(spriteA, spriteB) {
        var boundsA = spriteA.getBounds();
        var boundsB = spriteB.getBounds();
        return this.IsRectCross(boundsA, boundsB);
    }
    IsRectCross(rect1, rect2) {
        if (rect1.right - 45 <= rect2.left) return false;
        if (rect1.left + 45 >= rect2.right) return false;
        if (rect1.bottom - 196 <= rect2.top) return false;
        if (rect1.top >= rect2.bottom) return false;
        return true;
    }
    createCandies(left, right) {
        return this.time.addEvent({
            loop: true,
            delay: 1000,
            paused: true,
            callback: () => {
                if (!window.$phaserThis.isStartGame) {
                    return;
                }
                let i = Math.ceil(Math.random() * 12);
                let gift;
                if (i <= 4) {
                    gift = window.$phaserThis.bombs.create(0, 0, 'bomb').setScale(0.8);
                    gift.setTexture('bomb');
                    gift.setScale(0.8);
                    gift.setGravityY(500);
                } else if (i >= 5 && i <= 7) {
                    gift = window.$phaserThis.silveGifts.create(0, 0, 'bomb').setScale(0.8);
                    gift.setTexture('european_gas');
                    gift.setScale(0.8);
                    gift.setGravityY(500);
                } else {
                    gift = window.$phaserThis.silveGifts.create(0, 0, 'bomb').setScale(0.8);
                    gift.setTexture('blessing');
                    gift.setScale(0.8);
                    gift.setGravityY(500);
                }
                gift.setX(window.$phaserThis.between(left, right));
                gift.setY(window.$phaserThis.between(400, 580));
                gift.angle = window.$phaserThis.between(0, 0);
            }
        });
    }
    countDown() {
        //mask
        this.mask = this.add.renderTexture(0, 0, this.wWidth, this.actualHeight).setScale(4, 1);
        this.mask.fill('#b2463b', 0.5);
        this.mask.setDepth(6)

        console.log("重新开始游戏")
        //runing
        this.time.addEvent({
            repeat: 180,
            loop: false,
            delay: 10,
            callback: () => {
                if (this.silveGifts) {
                    this.silveGifts.children.entries.forEach((item) => {
                        item.destroy();
                    });
                }
                if (this.bombs) {
                    this.bombs.children.entries.forEach((item) => {
                        item.destroy();
                    });
                }
            }
        });
        //倒计时动效
        this.isDisplayGift = true;
        let indexIn = 0;
        let indexOut = 0;
        const scaleBigTime = 200; //放大时间
        const scaleSmallTime =200; //缩小时间
        const scaleBigDelay = 100; //停顿时间
        const intervalTime = (scaleBigTime + scaleSmallTime + scaleBigDelay) * 2; //倒计时动效定时时间
        let that = this;
        if (indexIn >= 2 || indexOut >= 2) {
            return;
        }

        this.time.addEvent({
            repeat: 2,
            loop: false,
            delay: intervalTime,
            callback: () => {
                let tweenObj = this.add.image(540, this.actualHeight - 1200, `time${this.aniArr[indexOut]}`);
                tweenObj.setDepth(7)

                this.add.tween({
                    targets: tweenObj.setScale(1),
                    scaleX: 0.9, // 恢复缩放原来的尺寸
                    scaleY: 0.9, // as the same
                    duration: scaleSmallTime, // 动画时间
                    // alpha: 0,                   // 透明度
                    ease: 'Power2',
                    repeat: 0,
                    completeDelay: scaleBigDelay, // 动画完成前的停顿时间
                    onComplete: function () {
                        // 动画完成后的回调
                        that.add.tween({
                            targets: tweenObj.setScale(1),
                            scaleX: 1.1,
                            scaleY: 1.1,
                            duration: scaleBigTime, // 动画时间
                            alpha: 0, // 透明度
                            ease: 'Power2',
                            repeat: 0
                        });
                        indexIn++;
                    }
                });
                indexOut++;
                if (indexIn > 2 || indexOut > 2) {
                    let that = this;
                    this.purse.on('drag', function (pointer, x, y) {
                        let minX = 150
                        let maxX = 930
                        if(x <= minX){
                            x = minX
                        }
                        if(x >= maxX) {
                            x = maxX
                        }
                        // console.log(x,'xxxxxxxxxxxx')
                        that.purse.x = x;
                    });
                    this.back.on('pointerdown', function (pointer, x, y) {
                        Router.back();
                    });

                    this.isDisplayGift = false;
                    this.isStartGame = true;
                    this.timedEvent = this.time.addEvent({
                        delay: 1000,
                        repeat: this.count
                    }); //开始倒计时动画结束,初始化游戏倒计时
                    try {
                        if (!this.isMusic) {
                            //防止网络慢的情况下加载失败
                            let that = this;
                            //初始化开启背景音乐
                            this.sound.play('bgMusic', {
                                valume: 0.75,
                            });
                            //背景音乐关闭打开
                            //生成打开音乐物理引擎
                            this.openMusic = this.physics.add.sprite(134, 575, 'openMusic').setScale(1)
                            this.openMusic.setCollideWorldBounds(true);
                            this.openMusic.setInteractive();
                            this.input.setDraggable(this.openMusic);
                            //对openMusic进行按下事件监听
                            this.openMusic.on('pointerdown', function (pointer, x, y) {
                                //生成关闭音乐物理引擎
                                that.closeMusic = that.physics.add.sprite(134, 575, 'closeMusic').setScale(1)
                                that.closeMusic.setCollideWorldBounds(true);
                                that.closeMusic.setInteractive();
                                that.input.setDraggable(that.closeMusic);
                                that.isClickMusic = true
                                that.closeMusic.setDepth(4)
                                that.sound.pauseAll()
                                let thats = that
                                //对closeMusic进行按下事件监听
                                that.closeMusic.on('pointerdown', function (pointer, x, y) {
                                    thats.closeMusic.destroy()
                                    thats.sound.play('bgMusic', {
                                        valume: 0.75,
                                    });
                                });
                            });

                        }
                        this.generatorTimerLeft.paused = false;
                        this.generatorTimerMiddie.paused = false;
                        this.generatorTimerRight.paused = false;
                    } catch (e) {}
                    this.mask.destroy();
                }
            }
        });
    }
    //掉礼包
    dropGift() {
        window.$phaserThis.bombs.children.entries.forEach((item) => {
            if (item.y * -1 > 3500 || item.y > 3500) {
                item.destroy();
            }
            if (window.$phaserThis.checkOverlap(window.$phaserThis.purse, item)) {
                if (this.animation1) {
                    this.animation1.destroy()
                }
                if (this.animation2) {
                    this.animation2.destroy()
                }
                this.animation2 = this.physics.add.sprite(window.$phaserThis.purse.x, this.actualHeight - 481, 'animation2').setScale(0.86);

                item.destroy();
                this.Store.commit('setCommonInfo', {
                    isClickBack: true,
                    isSignOut: false,
                    haveAward: this.isHaveAward ? true : false,
                    gamePopType: 'get-award'
                });
            }
        });
        window.$phaserThis.silveGifts.children.entries.forEach((item) => {
            if (item.y * -1 > 3500 || item.y > 3500) {
                item.destroy();
            }
            if (window.$phaserThis.checkOverlap(window.$phaserThis.purse, item)) {
                if (this.animation1) {
                    this.animation1.destroy()
                }
                if (this.animation2) {
                    this.animation2.destroy()
                }
                this.isHaveAward = true
                // console.log(window.$phaserThis.purse.x)
                this.animation1 = this.physics.add.sprite(window.$phaserThis.purse.x, this.actualHeight - 486, 'animation1').setScale(0.797);

                setTimeout(() => {
                    this.animation1.destroy()
                }, 50);
                item.destroy();
            }
        });
        window.$phaserThis.count = window.$phaserThis.timedEvent.repeatCount;
        if (window.$phaserThis.count === 0) window.$phaserThis.count = 100000;
    } // dropGift
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444

countDown()实现了游戏3秒倒计时的功能
在这里插入图片描述

//生成底部袋子引擎
this.purse = this.physics.add.sprite(540, this.actualHeight - 401, 'purse').setScale(0.797)
//监听袋子的拖拽事件
this.purse.on('drag', function (pointer, x, y) {
                 let minX = 150
                 let maxX = 930
                 if(x <= minX){
                     x = minX
                 }
                 if(x >= maxX) {
                     x = maxX
                 }
                 that.purse.x = x;
  });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

上面的代码实现了底部袋子的左右拖拽功能
在这里插入图片描述

//初始化开启背景音乐
this.sound.play('bgMusic', {
    valume: 0.75,
});
//背景音乐关闭打开
//生成打开音乐物理引擎
this.openMusic = this.physics.add.sprite(134, 575, 'openMusic').setScale(1)
this.openMusic.setCollideWorldBounds(true);
this.openMusic.setInteractive();
this.input.setDraggable(this.openMusic);
//对openMusic进行按下事件监听
this.openMusic.on('pointerdown', function (pointer, x, y) {
    //生成关闭音乐物理引擎
    that.closeMusic = that.physics.add.sprite(134, 575, 'closeMusic').setScale(1)
    that.closeMusic.setCollideWorldBounds(true);
    that.closeMusic.setInteractive();
    that.input.setDraggable(that.closeMusic);
    that.isClickMusic = true
    that.closeMusic.setDepth(4)
    that.sound.pauseAll()
    let thats = that
    //对closeMusic进行按下事件监听
    that.closeMusic.on('pointerdown', function (pointer, x, y) {
        thats.closeMusic.destroy()
        thats.sound.play('bgMusic', {
            valume: 0.75,
        });
    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

这段代码实现了点击音乐可以进行背景音乐的开启和关闭的切换功能
在这里插入图片描述

1.3整合game.js

import Phaser from 'phaser';
import BootScene from './BootScene';
import PlayScene from './PlayScene';

function launch(containerId, self) {
    return new Phaser.Game({
        parentPoint: self,
        type: Phaser.CANVAS,
        parent: containerId,
        scale: {
            mode: Phaser.Scale.WIDTH_CONTROLS_HEIGHT,
            width: 1080,
            height: window.innerHeight * 1080 / window.innerWidth
        },
        backgroundColor: '#b2463b',
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 0 },
                debug: false
            }

        },
        scene: [BootScene, PlayScene]
    });
}

export default launch
export { launch }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

1.4js使用,在vue中的mounted生命周期中,引入game.js

    const game = await import('@src/game/game');
    this.$nextTick(() => {
        this.gameInstance = game.launch(this.containerId, this);
    });
  • 1
  • 2
  • 3
  • 4

1.5.主要实现截取下落物体的小游戏

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/991780
推荐阅读
  

闽ICP备14008679号