cocos creator 滑动屏幕 移动控件

import { _decorator, Component, Input,input,UITransform,CameraComponent, Node, EventTouch, Vec3} from 'cc';

const { ccclass, property } = _decorator;

@ccclass('NewComponent')

export class NewComponent extends Component {

@property({displayName: "相机", tooltip: "", type: CameraComponent})

相机: CameraComponent = null!;

@property({displayName: "目标节点", tooltip: "", type: Node})

目标节点: Node = null!;

start() {

input.on(Input.EventType.TOUCH_MOVE, this.屏幕触摸移动, this);

}

屏幕触摸移动(e: EventTouch){

//这个是屏幕坐标

var 屏幕触摸点=e.getLocation();

// 屏幕坐标转为世界坐标 注意这里用的节点 相机

let 世界坐标 = this.相机.screenToWorld(new Vec3(屏幕触摸点.x,屏幕触摸点.y,0));

//方法一 设置目标位置 这个函数是 设置世界坐标

this.目标节点.setWorldPosition(世界坐标);

/*

//方法二 设置目标位置 这个函数是 设置世界坐标

var 父节点=this.node.getParent();//获取父节点

//用父节点的 UITransform控件

var 节点的UITransform控件=父节点.getComponent(UITransform);

let 新坐标=节点的UITransform控件.convertToNodeSpaceAR(世界坐标);

//设置本地坐标

this.目标节点.setPosition(新坐标);

*/

}

update(deltaTime: number) {

}

}

点击看效果 https://www.58game.online/game/cocoscreator%e6%bb%91%e5%8a%a8%e5%b1%8f%e5%b9%95%e7%a7%bb%e5%8a%a8%e6%8e%a7%e4%bb%b6/

源码关注公众号领取

https://download.csdn.net/download/qq_30121869/87396532

https://www.58game.online/

猜你喜欢

转载自blog.csdn.net/qq_30121869/article/details/128759663