cocos creator制作弹窗有小到大的动画显示

参考:https://www.bilibili.com/video/BV1kJ411u72v?p=2

1.选择要生成动画的弹窗 》添加组件 》其他组件 》Animation

结果就是这样的

这里看到需要 animation-clip 文件,下面制作animation-clip文件

2.在 assets 中新建文件夹 animClip , 专门用来存放 animation-clip 文件的,名字可以随便取

右键 》新建 》Animation Clip,这里我们需要用到两个动画,所以我新建了两个animation-clip文件

3.动画制作

4.开始由无到有的播放,代码

cc.Class({
    extends: cc.Component,

    properties: {
        scrollView: {
            default: null,
            type: cc.ScrollView
        },
        ruleTip: {
            default: null,
            type: cc.Node
        }
        // scrollView: cc.ScrollView
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.ruleTip = this.ruleTip.getComponent(cc.Animation);
        this.ruleTip.node.active = false;   //这里要node是因为this.ruleTip = this.ruleTip.getComponent(cc.Animation);这个重新获取了Animation,
                                            // 如果this.ruleTip.node.active = false;
                                            // 放在this.ruleTip = this.ruleTip.getComponent(cc.Animation);这句的上面就不用node了

    },

    bottonClick(target, data) {
        this.ruleTip.node.active = true;
        this.ruleTip.play('scaleToShow')
    },

5.隐藏,制作动画,就是将上面的显示的动画相反制作,这里就不讲解了

6.隐藏代码

注意:如果需要隐藏,是需要回调函数的

制作回调函数:

右键方块:点击编辑,在里面写上函数名称

这里可以添加很多函数

完整代码:

cc.Class({
    extends: cc.Component,

    properties: {
        scrollView: {
            default: null,
            type: cc.ScrollView
        },
        ruleTip: {
            default: null,
            type: cc.Node
        }
        // scrollView: cc.ScrollView
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.ruleTip = this.ruleTip.getComponent(cc.Animation);
        this.ruleTip.node.active = false; //这里要node是因为this.ruleTip = this.ruleTip.getComponent(cc.Animation);这个重新获取了Animation,如果this.ruleTip.node.active = false;放在this.ruleTip = this.ruleTip.getComponent(cc.Animation);这句的上面就不用node了
        this.ruleTip.scaleToHidePlayEnd = function() {
            this.ruleTip.node.active = false;
        }.bind(this)

    },

    bottonClick(target, data) {
        if (data == 'help') {
            this.ruleTip.node.active = true;
            this.ruleTip.play('scaleToShow')
        } else if (data == 'close') {
            this.ruleTip.play('scaleToHide');
        }
    },

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator中使用TypeScript来创建弹窗控制器,你可以按照以下步骤进行操作: 1. 创建一个名为PopupManager的类,该类将负责管理所有弹窗的打开和关闭操作。 ```typescript export class PopupManager { private static _instance: PopupManager = null; private _popupStack: cc.Node[] = []; private _popupLayer: cc.Node = null; public static getInstance(): PopupManager { if (PopupManager._instance == null) { PopupManager._instance = new PopupManager(); } return PopupManager._instance; } private constructor() { this._popupLayer = new cc.Node(); this._popupLayer.name = "PopupLayer"; cc.director.getScene().addChild(this._popupLayer); } public pushPopup(popup: cc.Node) { if (popup == null) { return; } this._popupLayer.addChild(popup); this._popupStack.push(popup); } public popPopup() { if (this._popupStack.length > 0) { let popup = this._popupStack.pop(); popup.removeFromParent(true); } } } ``` 2. 创建一个名为PopupBase的类,该类将作为所有弹窗的基类,提供弹窗的打开和关闭方法。你可以在该类的onLoad方法中进行初始化操作。 ```typescript export class PopupBase extends cc.Component { public onLoad() { // 初始化弹窗界面 this.initView(); } public initView() { // 子类需要实现该方法,用于初始化弹窗界面 } public show() { // 弹出弹窗 PopupManager.getInstance().pushPopup(this.node); } public hide() { // 隐藏弹窗 PopupManager.getInstance().popPopup(); } } ``` 3. 创建一个名为MyPopup的类,该类将作为你的具体弹窗控制器,在该类中继承PopupBase类。 ```typescript export class MyPopup extends PopupBase { private _closeBtn: cc.Node = null; public initView() { // 初始化弹窗界面 this._closeBtn = this.node.getChildByName("CloseBtn"); this._closeBtn.on(cc.Node.EventType.TOUCH_END, this.onCloseBtnClick, this); } private onCloseBtnClick() { // 关闭弹窗 this.hide(); } } ``` 4. 在场景中添加一个空节点作为弹窗容器,并将MyPopup预制体添加到该节点下。 ```typescript const {ccclass, property} = cc._decorator; @ccclass export class MainScene extends cc.Component { @property(cc.Prefab) private _myPopupPrefab: cc.Prefab = null; private _myPopup: cc.Node = null; public onLoad() { // 加载弹窗预制体 this._myPopup = cc.instantiate(this._myPopupPrefab); } public onBtnShowPopupClick() { // 显示弹窗 this._myPopup.getComponent(MyPopup).show(); } } ``` 现在你已经创建了一个弹窗控制器,可以通过该控制器来方便地管理多个弹窗,并在需要的时候打开或关闭它们。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值