主页 > canvas引擎 > Phaser游戏引擎 >
来源:未知 时间:2023-12-13 14:21 作者:小飞侠 阅读:次
[导读] phaser3图片精灵人物每个状态动画,请点击下方画布看看效果。 代码讲解: classExampleextendsPhaser.Scene{constructor(){super();}preload(){//在预加载里面加装图片精灵,由48*48组成每个状态的动画集...
phaser3图片精灵人物每个状态动画,请点击下方画布看看效果。 代码讲解: class Example extends Phaser.Scene { constructor () { super(); } preload () { // 在预加载里面加装图片精灵,由48*48组成每个状态的动画集合 this.load.spritesheet('brawler', 'brawler48x48.png', { frameWidth: 48, frameHeight: 48 }); this.load.image('grid', 'grid-ps2.png'); } create () { // Text section this.add.tileSprite(400, 300, 800, 600, 'grid'); this.add.image(0, 0, 'brawler', '__BASE').setOrigin(0, 0); this.add.grid(0, 0, 192, 384, 48, 48).setOrigin(0, 0).setOutlineStyle(0x00ff00); this.add.text(200, 24, '<- walk', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 72, '<- idle', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 120, '<- kick', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 168, '<- punch', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 216, '<- jump', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 264, '<- jump kick', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 312, '<- win', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(200, 360, '<- die', { color: '#00ff00' }).setOrigin(0, 0.5); this.add.text(48, 440, 'Click to change animation', { color: '#00ff00' }); const current = this.add.text(48, 460, 'Playing: walk', { color: '#00ff00' }); // 创建每个状态动画,后续会根据key属性执行每组动画 this.anims.create({ key: 'walk', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 0, 1, 2, 3 ] }), frameRate: 8, repeat: -1 }); this.anims.create({ key: 'idle', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 5, 6, 7, 8 ] }), frameRate: 8, repeat: -1 }); this.anims.create({ key: 'kick', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 10, 11, 12, 13, 10 ] }), frameRate: 8, repeat: -1, repeatDelay: 2000 }); this.anims.create({ key: 'punch', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 15, 16, 17, 18, 17, 15 ] }), frameRate: 8, repeat: -1, repeatDelay: 2000 }); this.anims.create({ key: 'jump', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 20, 21, 22, 23 ] }), frameRate: 8, repeat: -1 }); this.anims.create({ key: 'jumpkick', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 20, 21, 22, 23, 25, 23, 22, 21 ] }), frameRate: 8, repeat: -1 }); this.anims.create({ key: 'win', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 30, 31 ] }), frameRate: 8, repeat: -1, repeatDelay: 2000 }); this.anims.create({ key: 'die', frames: this.anims.generateFrameNumbers('brawler', { frames: [ 35, 36, 37 ] }), frameRate: 8, }); const keys = [ 'walk', 'idle', 'kick', 'punch', 'jump', 'jumpkick', 'win', 'die' ]; const cody = this.add.sprite(600, 370); cody.setScale(8); cody.play('walk'); let c = 0; // 每次点击切换不同动画 this.input.on('pointerdown', function () { c++; if (c === keys.length) { c = 0; } cody.play(keys[c]); current.setText('Playing: ' + keys[c]); }); } } const config = { type: Phaser.AUTO, parent: 'phaser-example', width: 800, height: 600, pixelArt: true, scene: Example }; const game = new Phaser.Game(config); |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com