【Cocos Creator】UITabEx.ts

import UITools from "./UITools";


const {ccclass, property} = cc._decorator;

@ccclass
export default class UITabEx extends cc.Component {

    @property(cc.Node)
    objCommom: cc.Node = null;

    @property(cc.Node)
    objPress: cc.Node = null;

    callback: Function;
    target:any;

    mState:boolean;

    addButtonEvent(callback: Function, target:any){
        this.callback = callback;
        this.target = target;
    }

    onLoad() {

        let btn: cc.Button = this.getComponent("Button") as cc.Button;
        if(btn == null)
            btn = this.addComponent(cc.Button);
        UITools.AddButtonEvent(btn,this.onBtnClick,this);
        this.UnTab();
    }

    onBtnClick(){
        if(this.mState == false)
        {
            this.OnclickWithoutMusic();
        }
    }

    OnTab()
	{
        UITools.SetActive(this.objPress,true);
        UITools.SetActive(this.objCommom,false);

        if(this.callback != null)
            this.callback.call(this.target);
		this.mState = true;
	}

	UnTab()
	{
        UITools.SetActive(this.objPress,false);
        UITools.SetActive(this.objCommom,true);
        this.mState = false;
	}

    /// <summary>
    /// 脚本 点击btn
    /// </summary>
	public  OnclickWithoutMusic()
	{
		this.Set(true);
	}

    Set( state:boolean)
	{
		if (state)
		{
			if (this.node.parent != null)
			{
                var children = this.node.parent.children;
                if(children != null)
                {
                    for (var i = 0;i < children.length;i ++)
                    {
                        let script: UITabEx = children[i].getComponent("UITabEx") as UITabEx; 
                        if(script != null)    
                        script.UnTab(); 
                    }
                }
			}
		}

		if (state)
			this.OnTab();
	}
}

使用方法:

她们的父节点下,不要有其他node。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值