cocos creator 实现switch按钮(开关按钮)

cocos creator开关按钮动画实现

1.首先最简单的实现方式(没有动画),可以通过复选框
    1.1当点击复选框为选择时,可以更改图片,显示为开
    1.2档复选框取消时,图片更改为关
2.用tween动画实现
    2.1用mask来进行遮挡
    2.2通过变量进行判断,点击后相应的按钮进行移动.
    如下图所示gif

请添加图片描述
请添加图片描述

cocos switch实现、cocos creator switch按钮实现、cocos 开关按钮实现、cocos switch切换按钮实现]、cocos动画switch切换

(也可以通过复选框进行切换)

原代码在下方,免费下载无需积分

节点目录:
请添加图片描述

脚本实现:

import { _decorator, Component, Node, tween, Vec3, UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SwitchController')
export class SwitchController extends Component {
    @property(Node)
    SwitchButton:Node = null;
    @property(Node)
    Off:Node = null;
    @property(Node)
    On:Node = null;
    @property(Node)
    Switch:Node = null;
    @property(Boolean)
    SwitchFlag:Boolean = true;

    radius = 0;
    radiusSwitch = 0;

    onLoad(){
        this.radius = this.SwitchButton.getComponent(UITransform).contentSize.width;
        this.radiusSwitch = this.SwitchButton.getComponent(UITransform).contentSize.width/2;

        this.switchHandle();
        this.SwitchButton.on(Node.EventType.TOUCH_END,()=>{
            this.SwitchFlag = !this.SwitchFlag;      
            this.switchHandle();
        })
    }

    switchHandle(){
        if(!this.SwitchFlag){
            tween(this.Off)
            .to(0.3, {position: new Vec3(this.SwitchButton.position.x,this.Off.position.y, 10)})
            .start();
            tween(this.Switch)
            .to(0.3, {position: new Vec3(this.radiusSwitch,this.On.position.y, 10)})
            .start();
            tween(this.On)
            .to(0.3, {position: new Vec3(this.radius,this.On.position.y, 0)})
            .start();
        }else{
            tween(this.On)
            .to(0.3, {position: new Vec3(this.SwitchButton.position.x,this.On.position.y, 10)})
            .start();

            tween(this.Switch)
            .to(0.3, {position: new Vec3(-this.radiusSwitch,this.On.position.y, 10)})
            .start();
            
            tween(this.Off)
            .to(0.3, {position: new Vec3(-this.radius,this.Off.position.y, 0)})
            .start()
        }
    }

    start() {

    }

    update(deltaTime: number) {
        
    }
}


在这里插入图片描述

点击下载

https://download.csdn.net/download/mingketao/86927296

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值