phaser引擎使用记录

初始化game

复制代码
function gameInit(){
        game = new Phaser.Game({
            type: Phaser.WEBGL,//游戏渲染方式---手机游戏请务必指定Phaser.CANVAS     WEBGL
            parent:'game',//游戏容器
            // backgroundColor: '#2dab2d',
            transparent:true,//画布元素是否透明
            physics: {
                default: 'arcade',
                arcade: {
                    gravity: { y: 0 },
                    debug: false,
                    // fps:30
                }
            },
            scale: {
                mode: Phaser.Scale.FIT,
                parent: 'game',
                autoCenter: Phaser.Scale.CENTER_BOTH,
                width: gameWidth*2,
                height: gameHeight*2
            },
            // scene: [ GameSence ],
        });
        game.scene.add('GameSence', GameSence);
        game.scene.start("GameSence");
    }
  var GameSence = new Phaser.Class({
    Extends: Phaser.Scene,
        preload: function() {
                   this.load.on('progress',  (value)=> {
            //加载进度
        });
this.load.on('complete',  ()=> {
            //加载完成回调
        });

},//加载素材
        create:function(){},//创建场景
        update: function() {},//游戏实时监听
})

/*--------*/
this.scene.resume();//恢复场景
this.scene.pause();//暂停场景
this.scene.restart();//重新开始场景


this.load.image('name','path');//加载图片素材
this.load.atlas('name','imgPath','jsonPath');//加载json序列帧素材
this.load.audio('name','path');//加载音乐素材

this.add.image(x, y, name);添加素材到场景中


//注意素材雪碧图尺寸大小不要超过2048*2048,不然卡顿非常严重可以一套动作图生成多个json加图片,然后通过for循环和this.anims.generateFrameNames('name' + i)的方式进行将每个json使用push添加到一个数组中,在通过sort进行排序,这样最终得出来的一组动画数据就可以用到this.anims.create({})中啦
复制代码

定时器:

复制代码
timedEvent = this.time.addEvent({
                    delay: 200/this.gameSpeedScale,
                    callback: () => {
                        //需要执行的逻辑
                    },
                    loop: true
                });
timedEvent.remove(true);
                            // 清除定时器
复制代码

动画

复制代码
this.tweens.add({
            targets: name,
            x: x,
            y: y,
            scale: scale,
            duration: 4000,
            ease: 'Quad.easeIn',

        onComplete: () =>{
          add10Score.destroy();
        }

        });
复制代码

事件操作

this.input.on("pointermove", (pointer)=>{
            
            
        },this);

destroy();//销毁

this.road.anims.timeScale = 2; //改变anims动画播放速度,初始为1

posted @   橙子汁z  阅读(76)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示