export default class CCUtils { // node.addChild 当你已经有parent的情况下再addChild会出现看不到的问题 public static addChild(child: cc.Node, parent: cc.Node, needCleanUp: boolean = false): void { const lastParent: cc.Node = child.parent; if (lastParent != null) { child.removeFromParent(needCleanUp); } child.setParent(parent); } // node避免触发onEnable onDisable public static setActive(node: cc.Node, active: boolean): void { if (node.active == active) { return; } node.active = active; } public static findChild(node: cc.Node, name: string): cc.Node { if (name == '') { return node; } const splitArr: any[] = name.split('/'); let tempNode: cc.Node = node; for (let i: number = 0; i < splitArr.length; i++) { tempNode = tempNode.getChildByName(splitArr[i]); if (tempNode == null) { break; } } return tempNode; } public static getSprite(node: cc.Node, name: string): cc.Sprite { return this.findChild(node, name).getComponent(cc.Sprite); } public static getLabel(node: cc.Node, name: string): cc.Label { return this.findChild(node, name).getComponent(cc.Label); } public static getRichText(node: cc.Node, name: string): cc.RichText { return this.findChild(node, name).getComponent(cc.RichText); } public static getCanvas(node: cc.Node, name: string): cc.Canvas { return this.findChild(node, name).getComponent(cc.Canvas); } public static getWidget(node: cc.Node, name: string): cc.Widget { return this.findChild(node, name).getComponent(cc.Widget); } public static getLayout(node: cc.Node, name: string): cc.Layout { return this.findChild(node, name).getComponent(cc.Layout); } public static getScrollView(node: cc.Node, name: string): cc.ScrollView { return this.findChild(node, name).getComponent(cc.ScrollView); } public static getScrollBar(node: cc.Node, name: string): cc.Scrollbar { return this.findChild(node, name).getComponent(cc.Scrollbar); } public static getProgressBar(node: cc.Node, name: string): cc.ProgressBar { return this.findChild(node, name).getComponent(cc.ProgressBar); } public static getToggle(node: cc.Node, name: string): cc.Toggle { return this.findChild(node, name).getComponent(cc.Toggle); } public static getToggleContainer(node: cc.Node, name: string): cc.ToggleContainer { return this.findChild(node, name).getComponent(cc.ToggleContainer); } public static getPageView(node: cc.Node, name: string): cc.PageView { return this.findChild(node, name).getComponent(cc.PageView); } public static getPageviewIndicator(node: cc.Node, name: string): cc.PageViewIndicator { return this.findChild(node, name).getComponent(cc.PageViewIndicator); } public static getSlider(node: cc.Node, name: string): cc.Slider { return this.findChild(node, name).getComponent(cc.Slider); } public static getBlockInputEvents(node: cc.Node, name: string): cc.BlockInputEvents { return this.findChild(node, name).getComponent(cc.BlockInputEvents); } public static getGraphics(node: cc.Node, name: string): cc.Graphics { return this.findChild(node, name).getComponent(cc.Graphics); } public static getAnimation(node: cc.Node, name: string): cc.Animation { return this.findChild(node, name).getComponent(cc.Animation); } public static getPolygonCollider(node: cc.Node, name: string): cc.PolygonCollider { return this.findChild(node, name).getComponent(cc.PolygonCollider); } public static getBoxCollider(node: cc.Node, name: string): cc.BoxCollider { return this.findChild(node, name).getComponent(cc.BoxCollider); } public static getVideoPlayer(node: cc.Node, name: string): cc.VideoPlayer { return this.findChild(node, name).getComponent(cc.VideoPlayer); } public static isInBox(worldPos: cc.Vec2, boxCollider: cc.BoxCollider): boolean { const innerPos: cc.Vec2 = boxCollider.node.convertToNodeSpaceAR(worldPos); let isInX: boolean = false; let isInY: boolean = false; if (innerPos.x >= -boxCollider.node.anchorX * boxCollider.size.width && innerPos.x <= boxCollider.size.width * (1 - boxCollider.node.anchorX)) { isInX = true; } if (innerPos.y >= -boxCollider.node.anchorY * boxCollider.size.height && innerPos.y <= boxCollider.size.height * (1 - boxCollider.node.anchorY)) { isInY = true; } return isInX && isInY; } public static findLastNumber(name: string): number { const reg: RegExp = /.*([0-9]+)$/; const result: any = name.match(reg); if (result != null) { return Number(result[1]); } return -1; } public static changeToNodePos(node1: cc.Node, node2: cc.Node): cc.Vec2 { const worldPos: cc.Vec2 = node1.parent.convertToWorldSpaceAR(node1.getPosition()); const nodeContentPos: cc.Vec2 = node2.parent.convertToNodeSpaceAR(worldPos); return nodeContentPos; } public static getOrAddComponent(node: cc.Node, type: new () => T): T { const component: any = node.getComponent(type); if (component) { return component; } return node.addComponent(type); } // public static createRegion(texture) { // let skeletonTexture = new sp.SkeletonTexture() // skeletonTexture.setRealTexture(texture) // let page = new sp.spine.TextureAtlasPage() // page.name = texture.name // page.uWrap = sp.spine.TextureWrap.ClampToEdge // page.vWrap = sp.spine.TextureWrap.ClampToEdge // page.texture = skeletonTexture // page.texture.setWraps(page.uWrap, page.vWrap) // page.width = texture.width // page.height = texture.height // let region = new sp.spine.TextureAtlasRegion() // region.page = page // region.width = texture.width // region.height = texture.height // region.originalWidth = texture.width // region.originalHeight = texture.height // region.rotate = false // region.u = 0 // region.v = 0 // region.u2 = 1 // region.v2 = 1 // region.texture = skeletonTexture // return region // } public static shuffle(arr: Array): void { for (let i: number = arr.length - 1; i >= 1; i--) { const random: number = Math.floor(Math.random() * i); const temp: any = arr[i]; arr[i] = arr[random]; arr[random] = temp; } } public static getClassName(clas: any): string { const name: string = cc.js.getClassName(clas); return name; } public static getClassByName(name: string): Function { const clas: Function = cc.js.getClassByName(name); return clas; } public static setClass(className: string, clas: Function): void { cc.js.setClassName(className, clas); } }