cocoscreator模拟器怎么触摸
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocoscreator模拟器怎么触摸相关的知识,希望对你有一定的参考价值。
参考技术A ScrollView和Menu的触摸事件优先级有点冲突,Menu的值低优先级高,如果不改的话在Menu拖动的话没反应,这应该不是我们想要的。改了它的优先级之后,拖动可以了,但是拖动后也会触发Menu的点击。
!!!!!!!!肿么办据说可以阻断事件的传播,但是我不会弄,谁要会请告诉我。!!!!!!!!
变相实现了一下,在按钮的事件中判断ScrollView是否滚动到位,不到位啥也不干。
另外,ScrollView的默认调整被关掉了,自己写了一下,默认的只会自动调整头尾。本回答被提问者采纳
cocosCreator中的碰撞检测和触摸事件
const { ccclass, property } = cc._decorator; @ccclass export default class BallClass extends cc.Component { @property text: string = ‘Ball‘; @property(cc.Node) Ball: cc.Node = null; @property weight: number = null; /** 物体的默认排序 */ @property partIndex: number = null; /** 目标节点 */ // @property(cc.Node) // aimParentNode: cc.Node = null; @property(cc.Component) game: any = null; onLoad() { } start() { } init(game: cc.Component) { this.game = game; this.registerEvent(this.node); } registerEvent(ballNode: cc.Node) { ballNode.on(cc.Node.EventType.TOUCH_START, this.touchStart, this); ballNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this); ballNode.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this); } unRegisterEvent(ballNode: cc.Node) { ballNode.off(cc.Node.EventType.TOUCH_START, this.touchStart, this); ballNode.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this); ballNode.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this); } touchStart(event) { console.log(‘--触摸--start‘); if (!this.game.cheakRunning()) { return; } // let sp = this.getComponent(cc.Sprite); // console.log(‘sp.node:‘, sp.node) let Part = this.node.parent.getChildByName(‘Part‘); Part.opacity = 255; let target = event.target; target.scale = 1.5; target.zIndex = 1; } touchMove(event: cc.Event.EventTouch) { console.log(‘--触摸--move‘); if (!this.game.cheakRunning()) { return; } var posX = event.touch.getLocationX() - 640; var posY = event.touch.getLocationY() - 360; let target = event.target; let delta = event.touch.getDelta(); target.x += delta.x; target.y += delta.y; target.rotation = 0; target.zIndex = 1; } touchEnd(event) { console.log(‘--触摸--end‘, this); if (false && !this.game.cheakRunning()) { // 回归原位 console.log(‘---没时间了--‘); this.backToOriginal(event.target); return; } console.log(‘end-pos‘, event); this.checkCollection(event.target); } checkCollection(ballNode: cc.Node) { console.log(‘--检测碰撞--‘, ballNode); console.log(‘--世界坐标系--‘, ballNode.convertToWorldSpace(ballNode.getPosition())) let target: cc.Node = ballNode; let targetParent = target.parent; let x = target.x + targetParent.x + targetParent.parent.x; let y = target.y + targetParent.y + targetParent.parent.y; let ball = target.getComponent(‘Ball‘); console.log(ball); let aimNode = this.game.coacheList[ball.weight]; let aimX = aimNode.x + aimNode.parent.x + aimNode.parent.parent.x; let aimY = aimNode.y + aimNode.parent.y + aimNode.parent.parent.y; let distance = Math.sqrt((x - aimX) * (x - aimX) + (y - aimY) * (y - aimY)) console.log({ aimX, aimY, targetX: target.x, targetY: target.y, distance, x, y }) if (distance < (target.height + ballNode.height) / 2) { if (!target.isChildOf(aimNode)) { target.parent = aimNode; target.position = cc.v2(0, -10); target.scale = 1; target.zIndex = 1; this.unRegisterEvent(target); this.game.addScore(); } } else { this.backToOriginal(ballNode); } } /** * 回到原来的位置 * @param ballNode */ backToOriginal(ballNode: cc.Node) { let ball: BallClass = ballNode.getComponent(‘Ball‘); let moveTo = cc.moveTo(0.3, cc.v2(0, 0)); // 此处默认0点位置 let callFunc = cc.callFunc(() => { let Part = ballNode.parent.getChildByName(‘Part‘); Part.opacity = 0; }); let sequence = cc.sequence(moveTo, callFunc); ballNode.runAction(sequence); ballNode.scale = 1; ballNode.zIndex = 1; } // update (dt) {} }
以上是关于cocoscreator模拟器怎么触摸的主要内容,如果未能解决你的问题,请参考以下文章