import { _decorator, Component, Node, RigidBody2D, Vec2 ,PhysicsSystem2D,UITransform,Vec3} from 'cc'; const { ccclass, property } = _decorator; @ccclass('test') export class test extends Component { @property({type:Node}) leftPlayer:Node; // leftPlayerSprite:Node; ball:Node; start() { // PhysicsSystem2D.instance.enable = true; // PhysicsSystem2D.instance.gravity = new Vec2(0,-50); console.log("===============") this.ball=this.node.getChildByName("Ball") this.ball.getComponent(RigidBody2D).linearVelocity = new Vec2(40,0) // this.leftPlayerSprite=this.leftPlayer.getChildByName("Sprite") this.node.on(Node.EventType.TOUCH_START, this._onTouchStart, this); this.node.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this); this.node.on(Node.EventType.TOUCH_END, this._onTouchEnd, this); } newPos:Vec3; isUpdatePos:boolean; _onTouchStart(touchEvent) { } _onTouchEnd(touchEvent) { } _onTouchMove(touchEvent) { console.log("===============_onTouchMove") // this.leftPlayer.getChildByName("Sprite").getComponent(RigidBody2D).enabled=false; let location = touchEvent.getUILocation(); this.newPos = this.leftPlayer.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(location.x,location.y,0)); // 确定位置 let newY=Math.min(480,Math.max(this.newPos.y,-480)) this.leftPlayer.position=new Vec3(-292,newY,0) // this.scheduleOnce(() => { // this.leftPlayer.position = this.newPos // }, 0); //console.log("==_onTouchMove=="); // this.leftPlayer.getChildByName("Sprite").getComponent(RigidBody2D).enabled=true; } update(deltaTime: number) { // if(this.isUpdatePos){ // console.log("===============isUpdatePos") // this.leftPlayer.position = this.newPos // // this.isUpdatePos=false // } if(this.ball.position.x<=-340 || this.ball.position.x>=320 ) { console.log("重置") this.ball.position=new Vec3(0,0,0) this.ball.getComponent(RigidBody2D).linearVelocity = new Vec2(40,0) } } }