[add] HoldButton 防止跟Button事件衝突

This commit is contained in:
建喵 2022-05-02 22:27:29 +08:00
parent d012d3a6a1
commit 4a033f606c
6 changed files with 93 additions and 20 deletions

View File

@ -78,10 +78,10 @@
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 17 "__id__": 19
}, },
{ {
"__id__": 18 "__id__": 20
} }
], ],
"_prefab": null, "_prefab": null,
@ -251,10 +251,13 @@
"__id__": 13 "__id__": 13
}, },
{ {
"__id__": 14 "__id__": 15
}, },
{ {
"__id__": 15 "__id__": 16
},
{
"__id__": 18
} }
], ],
"_prefab": null, "_prefab": null,
@ -665,7 +668,11 @@
"_grayMaterial": null, "_grayMaterial": null,
"duration": 0.1, "duration": 0.1,
"zoomScale": 0.9, "zoomScale": 0.9,
"clickEvents": [], "clickEvents": [
{
"__id__": 14
}
],
"_N$interactable": true, "_N$interactable": true,
"_N$enableAutoGrayEffect": false, "_N$enableAutoGrayEffect": false,
"_N$transition": 1, "_N$transition": 1,
@ -725,6 +732,16 @@
}, },
"_id": "b1JdTzpetM5Y2+ajB7CgHk" "_id": "b1JdTzpetM5Y2+ajB7CgHk"
}, },
{
"__type__": "cc.ClickEvent",
"target": {
"__id__": 5
},
"component": "",
"_componentId": "cf85bcS8qVLQ5grFSK4g1D6",
"handler": "Log",
"customEventData": "5555"
},
{ {
"__type__": "cc.Sprite", "__type__": "cc.Sprite",
"_name": "", "_name": "",
@ -778,7 +795,7 @@
}, },
"OnInvoke": [ "OnInvoke": [
{ {
"__id__": 16 "__id__": 17
} }
], ],
"_id": "42FPUDXnROx55pRRLBeL22" "_id": "42FPUDXnROx55pRRLBeL22"
@ -786,12 +803,22 @@
{ {
"__type__": "cc.ClickEvent", "__type__": "cc.ClickEvent",
"target": { "target": {
"__id__": 6 "__id__": 5
}, },
"component": "", "component": "",
"_componentId": "cc.Label", "_componentId": "cf85bcS8qVLQ5grFSK4g1D6",
"handler": "destroy", "handler": "Log",
"customEventData": "false" "customEventData": "6666"
},
{
"__type__": "cf85bcS8qVLQ5grFSK4g1D6",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": true,
"_id": "59e/1WbXJCBrpkHVzwqEeX"
}, },
{ {
"__type__": "cc.Canvas", "__type__": "cc.Canvas",

View File

@ -113,7 +113,6 @@ export class Badminton extends cc.Component {
//#region 初始化 //#region 初始化
protected onLoad(): void { protected onLoad(): void {
// this.node.getChildByName("Test").getComponent(HoldButton).OnInvoke.AddListener(() => { this.OnChangeAvatar(666); });
Badminton._instance = this; Badminton._instance = this;
new LocalStorageData(); new LocalStorageData();
this.config.Init(); this.config.Init();
@ -173,14 +172,13 @@ export class Badminton extends cc.Component {
let picObj: cc.Node = item.node.getChildByName("Avatar").getChildByName("Pic"); let picObj: cc.Node = item.node.getChildByName("Avatar").getChildByName("Pic");
if (picObj != null) { if (picObj != null) {
picObj.getComponent(cc.Sprite).spriteFrame = this.config.GetAvatarPicById(this.TeamMemberList[idx].AvatarId); picObj.getComponent(cc.Sprite).spriteFrame = this.config.GetAvatarPicById(this.TeamMemberList[idx].AvatarId);
let EventHandler: cc.Component.EventHandler = new cc.Component.EventHandler(); // let EventHandler: cc.Component.EventHandler = new cc.Component.EventHandler();
EventHandler.target = this.node; // EventHandler.target = this.node;
EventHandler.component = this.name.split("<")[1].split(">")[0]; // EventHandler.component = this.name.split("<")[1].split(">")[0];
EventHandler.handler = "OnChangeAvatar"; // EventHandler.handler = "OnChangeAvatar";
EventHandler.customEventData = idx.toString(); // EventHandler.customEventData = idx.toString();
picObj.getComponent(HoldButton).OnInvoke.push(EventHandler); // picObj.getComponent(HoldButton).OnInvoke.push(EventHandler);
// picObj.getComponent(HoldButton).OnInvoke.AddListener(this.OnChangeAvatar(idx)); picObj.getComponent(HoldButton).OnInvoke.AddListener(() => { this.OnChangeAvatar(idx); });
// picObj.getComponent(HoldButton).OnInvoke.AddListener(() => { this.OnChangeAvatar(idx); });
picObj.on("click", () => { item.isChecked = !item.isChecked; }, this); picObj.on("click", () => { item.isChecked = !item.isChecked; }, this);
} }
this._m_toggleList.push(item); this._m_toggleList.push(item);

View File

@ -30,6 +30,11 @@ export default class HoldButton extends cc.Component {
//#region private //#region private
/** 按钮的点击事件列表 */
private _clickEvents: cc.Component.EventHandler[] = [];
private _isOnInvoke: boolean = false;
private _m_isMouseDown: boolean = false; private _m_isMouseDown: boolean = false;
private _m_pressDeltaTime: number = 0; private _m_pressDeltaTime: number = 0;
@ -39,6 +44,8 @@ export default class HoldButton extends cc.Component {
//#region Lifecycle //#region Lifecycle
protected start(): void { protected start(): void {
this._clickEvents = this.getComponent(cc.Button).clickEvents.slice(0);
this.getComponent(cc.Button).clickEvents.Clear();
if (this.HoldLine != null) { if (this.HoldLine != null) {
this.HoldLine.active = false; this.HoldLine.active = false;
} }
@ -79,6 +86,7 @@ export default class HoldButton extends cc.Component {
} }
if (this._m_pressDeltaTime > this.MaxTime) { if (this._m_pressDeltaTime > this.MaxTime) {
this._isOnInvoke = true;
this._m_isMouseDown = false; this._m_isMouseDown = false;
if (this.IsHaveHoldLine) { if (this.IsHaveHoldLine) {
this.HoldLine.active = false; this.HoldLine.active = false;
@ -116,6 +124,12 @@ export default class HoldButton extends cc.Component {
if (this.IsHaveHoldLine) { if (this.IsHaveHoldLine) {
this.HoldLine.active = false; this.HoldLine.active = false;
} }
if (!this._isOnInvoke) {
this._clickEvents.forEach((eventHandler: cc.Component.EventHandler) => {
eventHandler.emit([this.node.getComponent(cc.Button)]);
});
}
this._isOnInvoke = false;
this._checkHoldAutoStart(0); this._checkHoldAutoStart(0);
} }
@ -125,6 +139,12 @@ export default class HoldButton extends cc.Component {
if (this.IsHaveHoldLine) { if (this.IsHaveHoldLine) {
this.HoldLine.active = false; this.HoldLine.active = false;
} }
if (!this._isOnInvoke) {
this._clickEvents.forEach((eventHandler: cc.Component.EventHandler) => {
eventHandler.emit([this.node.getComponent(cc.Button)]);
});
}
this._isOnInvoke = false;
this._checkHoldAutoStart(0); this._checkHoldAutoStart(0);
} }

View File

@ -15,7 +15,7 @@ declare interface Array<T> {
*/ */
ObjectSort(asc?: boolean[], key?: string[]): any[]; ObjectSort(asc?: boolean[], key?: string[]): any[];
/** /**
* Array<cc.Component.EventHandler>使 * Array<cc.Component.EventHandler>forHoldButton使
* Add a non persistent listener to the UnityEvent. * Add a non persistent listener to the UnityEvent.
* @param call Callback function. * @param call Callback function.
*/ */

19
assets/Script/Test.ts Normal file
View File

@ -0,0 +1,19 @@
// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const { ccclass, property } = cc._decorator;
@ccclass
export default class Test extends cc.Component {
/**
* name
*/
public Log(a, b): void {
cc.log(b);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "1.0.8",
"uuid": "cf85b712-f2a5-4b43-982b-1522b88350fa",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}