47 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2025-04-24 17:03:28 +08:00
/**
*
* @author BrightLi
* @since 2020/5/3
*/
const {ccclass, property} = cc._decorator;
@ccclass
export default class SKSwitch extends cc.Component {
@property(cc.SpriteFrame)
offFrame:cc.SpriteFrame=null;
@property(cc.Boolean)
public isOn:boolean=true;
public changeBlock:Function;
private sprite:cc.Sprite=null;
private currentFrame:cc.SpriteFrame;
private touchStart(event:cc.Event.EventTouch){
this.isOn=!this.isOn;
this.refresh();
}
private refresh(){
if(this.isOn){
this.sprite.spriteFrame=this.currentFrame;
}else if(this.offFrame){
this.sprite.spriteFrame=this.offFrame;
}
if(this.changeBlock){
this.changeBlock(this);
}
}
onLoad () {
this.sprite=this.node.getComponent(cc.Sprite);
this.currentFrame=this.sprite.spriteFrame;
if(!this.isOn){
this.refresh();
}
this.node.on(cc.Node.EventType.TOUCH_START,this.touchStart.bind(this));
}
}