bullet.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { _decorator, Collider, Component, ITriggerEvent, Node } from 'cc';
  2. import { constant } from '../framework/constant';
  3. const { ccclass, property } = _decorator;
  4. const OUTOFRANGE = 50
  5. @ccclass('bullet')
  6. export class bullet extends Component {
  7. @property
  8. public bulletSpeed = 0;
  9. onEnable() {
  10. const collider = this.getComponent(Collider)
  11. collider.on('onTriggerEnter', this._onTriggerEnter, this)
  12. }
  13. onDisable() {
  14. const collider = this.getComponent(Collider)
  15. collider.off('onTriggerEnter', this._onTriggerEnter, this)
  16. }
  17. private _onTriggerEnter(event: ITriggerEvent) {
  18. const collisionGroup = event.otherCollider.getGroup();
  19. console.log('子弹');
  20. if (collisionGroup === constant.CollisionType.ENEMY_PLANE ) {
  21. this.node.destroy()
  22. }
  23. }
  24. update(deltaTime: number) {
  25. const pos = this.node.position;
  26. const moveLength = pos.x + this.bulletSpeed;
  27. this.node.setPosition(moveLength, pos.y, pos.z)
  28. if(moveLength > OUTOFRANGE){
  29. this.node.destroy()
  30. }
  31. }
  32. }