8 Phaser3-图片透明效果

效果展示

在这里插入图片描述

关键技术点

  • 元素使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值