test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { _decorator, Component, Node, RigidBody2D, Vec2 ,PhysicsSystem2D,UITransform,Vec3} from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('test')
  4. export class test extends Component {
  5. @property({type:Node})
  6. leftPlayer:Node;
  7. // leftPlayerSprite:Node;
  8. ball:Node;
  9. start() {
  10. // PhysicsSystem2D.instance.enable = true;
  11. // PhysicsSystem2D.instance.gravity = new Vec2(0,-50);
  12. console.log("===============")
  13. this.ball=this.node.getChildByName("Ball")
  14. this.ball.getComponent(RigidBody2D).linearVelocity = new Vec2(40,0)
  15. // this.leftPlayerSprite=this.leftPlayer.getChildByName("Sprite")
  16. this.node.on(Node.EventType.TOUCH_START, this._onTouchStart, this);
  17. this.node.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
  18. this.node.on(Node.EventType.TOUCH_END, this._onTouchEnd, this);
  19. }
  20. newPos:Vec3;
  21. isUpdatePos:boolean;
  22. _onTouchStart(touchEvent) {
  23. }
  24. _onTouchEnd(touchEvent) {
  25. }
  26. _onTouchMove(touchEvent) {
  27. console.log("===============_onTouchMove")
  28. // this.leftPlayer.getChildByName("Sprite").getComponent(RigidBody2D).enabled=false;
  29. let location = touchEvent.getUILocation();
  30. this.newPos = this.leftPlayer.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(location.x,location.y,0)); // 确定位置
  31. let newY=Math.min(480,Math.max(this.newPos.y,-480))
  32. this.leftPlayer.position=new Vec3(-292,newY,0)
  33. // this.scheduleOnce(() => {
  34. // this.leftPlayer.position = this.newPos
  35. // }, 0);
  36. //console.log("==_onTouchMove==");
  37. // this.leftPlayer.getChildByName("Sprite").getComponent(RigidBody2D).enabled=true;
  38. }
  39. update(deltaTime: number) {
  40. // if(this.isUpdatePos){
  41. // console.log("===============isUpdatePos")
  42. // this.leftPlayer.position = this.newPos
  43. // // this.isUpdatePos=false
  44. // }
  45. if(this.ball.position.x<=-340 || this.ball.position.x>=320 )
  46. {
  47. console.log("重置")
  48. this.ball.position=new Vec3(0,0,0)
  49. this.ball.getComponent(RigidBody2D).linearVelocity = new Vec2(40,0)
  50. }
  51. }
  52. }