效果展示
关键技术点
- 元素使用
setAlpha
设置透明度,可以设置整体透明、局部透明和动态透明
部分主要文件代码
BootScene.js
import {Scene} from 'phaser'
import bear from '@/game/assets/bear.png'
import barbarian from '@/game/assets/barbarian.png'
export default class BootScene extends Scene {
constructor() {
super({key: 'BootScene'});
}
preload() {
// 加载图片素材
this.load.image("bear", bear)
this.load.image("barbarian", barbarian)
}
create() {
this.scene.start('PlayScene')
}
}
PlayScene.js
import {Scene, Display} from 'phaser'
export default class PlayScene extends Scene {
constructor() {
super({key: 'PlayScene'})
}
create() {
this.barbarian = this.add.image(0,0,"barbarian").setOrigin(0)
this.bear1 = this.add.image(0,0,"bear")
this.bear2 = this.add.image(0,0,"bear")
this.bear3 = this.add.image(0,0,"bear")
this.bear4 = this.add.image(0,0,"bear")
this.bear5 = this.add.image(0,0,"bear")
// 顺序top left, top right, bottom left, bottom right,0为透明
// 效果为底部透明
this.bear1.setAlpha(1, 1, 0, 0)
// 整体透明
this.bear2.setAlpha(0.2)
// 效果为上部透明
this.bear3.setAlpha(0, 0, 1, 1)
// 左部透明
this.bear4.alphaTopLeft = 0;
// 多组透明效果切换
this.tweens.add({
targets: this.bear5,
alphaTopLeft: { value: 0, duration: 1000, ease: 'Power1' },
alphaBottomRight: { value: 0, duration: 1000, ease: 'Power1' },
alphaBottomLeft: { value: 0, duration: 1000, ease: 'Power1', delay: 500 },
yoyo: true,
loop: -1
});
Display.Align.In.Center(this.bear1, this.barbarian);
Display.Align.In.TopRight(this.bear2, this.barbarian);
Display.Align.In.TopLeft(this.bear3, this.barbarian);
Display.Align.In.TopCenter(this.bear4, this.barbarian);
Display.Align.In.BottomCenter(this.bear5, this.barbarian);
}
update() {
}
}
本节完整代码下载地址
https://github.com/biwow/phaser3/tree/main/code/8