Buttet.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Component, Node,Vec2,Vec3 ,SpriteFrame,find,tween,Tween,Collider2D,Contact2DType,IPhysics2DContact, Sprite} from 'cc';
  2. const { ccclass, property } = _decorator;
  3. const RADIAN = Math.PI / 180;
  4. import { Game } from './Game';
  5. import { Item } from './Item';
  6. @ccclass('Buttet')
  7. export class Buttet extends Component {
  8. @property([SpriteFrame])
  9. frames:SpriteFrame[]=[];
  10. rotation:number=-30;
  11. speed:number=800;
  12. isUse:boolean;
  13. targetIndex:number;
  14. game:Game;
  15. start() {
  16. console.log("targetIndex==>>"+this.targetIndex)
  17. this.game=find("Canvas/GameMainPanel/GameRoot/Game/GameBg/Game").getComponent(Game)
  18. this.isUse=true
  19. this.node.getChildByName("Sprite").getComponent(Sprite).spriteFrame=this.frames[this.targetIndex]
  20. // 注册单个碰撞体的回调函数
  21. let collider = this.node.getChildByName("Sprite").getComponent(Collider2D);
  22. if (collider) {
  23. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  24. }
  25. this.move()
  26. }
  27. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
  28. // 只在两个碰撞体开始接触时被调用一次
  29. // console.log('onBeginContact==>>'+otherCollider.tag);
  30. if(!this.isUse) return;
  31. if(otherCollider.tag==1){
  32. this.game.playGet()
  33. this.isUse=false;
  34. if(otherCollider.node.parent.isValid)
  35. otherCollider.node.parent.destroy()
  36. // otherCollider.node.parent.active=false
  37. if(this.node.isValid)
  38. this.node.destroy()
  39. let otherIndex=otherCollider.node.parent.getComponent(Item).index
  40. console.log('onBeginContact2==>>'+this.node.isValid+","+otherIndex+","+this.targetIndex);
  41. if(this.targetIndex==otherIndex)
  42. {
  43. this.game.getItem(true)
  44. }else{
  45. this.game.getItem(false)
  46. }
  47. }
  48. }
  49. move(){
  50. //计算终点
  51. let endPoint = new Vec2();
  52. endPoint.x = 1000 * Math.sin(this.rotation);// * RADIAN);
  53. endPoint.y = 1000 * Math.cos(this.rotation);// * RADIAN);
  54. // console.log("_emmitNode==2>>"+endPoint)
  55. // let tmpVec= new Vec3(endPoint.x,endPoint.y,0).subtract(this.node.position).normalize();
  56. // let normalizeVec=new Vec2(tmpVec.x,tmpVec.y);
  57. // this.node.angle=new Vec2(0, 1).signAngle(normalizeVec) * 180 / Math.PI;
  58. let distance = new Vec3(endPoint.x,endPoint.y,0).subtract(this.node.position).length();
  59. let duration = distance / this.speed;
  60. tween(this.node)
  61. .to(duration, {
  62. position: new Vec3(endPoint.x,endPoint.y,0),
  63. }, {
  64. onComplete: (target?: object) => { // 回调,当缓动动作更新时触发。
  65. this.node.destroy()
  66. },
  67. })
  68. .start()
  69. }
  70. protected onDisable(): void {
  71. Tween.stopAllByTarget(this.node);
  72. }
  73. update(deltaTime: number) {
  74. }
  75. }