功能需求
Cocos Creator 开发斗地主,滑动选取手上的手牌
问题:Cocos Creator没有滑动选取节点的组件,于是自己造轮子
说明:DragChoose组件用于在多个节点上滑动时,选择鼠标滑动区域内的节点,由于Cocos Creator 支持TypeScript,所以该组件由TypeScript语言编写,需要配合Layout组件使用,可以直接挂在相应的Node节点上
使用示例
扑克根节点上需要挂上Layout组件,对子节点下的扑克牌进行排列
选择扑克,被选中的变成灰色
let dragChoose = this.node.addComponent(DragChoose)
dragChoose.OnMoving = (nodes) => {
for (let index = 0; index < nodes.length; index++)
{
const poker = nodes[index];
// poker 变成灰色
}
}
选择扑克结束,扑克弹起
let dragChoose = this.node.addComponent(DragChoose)
dragChoose.OnEnd = (nodes) => {
for (let index = 0; index < nodes.length; index++) {
const poker = nodes[index];
// poker 弹起
}
}
组件代码
const { ccclass, property } = cc._decorator;
@ccclass
export default class DragChoose extends cc.Component {
editor: {
executeInEditMode: true,
requireComponent: cc.Layout,
}
publ