bullet.js 644 B

1234567891011121314151617181920212223242526272829303132333435
  1. import Sprite from '../base/sprite'
  2. import DataBus from '../databus'
  3. const BULLET_IMG_SRC = 'images/bullet.png'
  4. const BULLET_WIDTH = 16
  5. const BULLET_HEIGHT = 30
  6. const __ = {
  7. speed: Symbol('speed')
  8. }
  9. const databus = new DataBus()
  10. export default class Bullet extends Sprite {
  11. constructor() {
  12. super(BULLET_IMG_SRC, BULLET_WIDTH, BULLET_HEIGHT)
  13. }
  14. init(x, y, speed) {
  15. this.x = x
  16. this.y = y
  17. this[__.speed] = speed
  18. this.visible = true
  19. }
  20. // 每一帧更新子弹位置
  21. update() {
  22. this.y -= this[__.speed]
  23. // 超出屏幕外回收自身
  24. if (this.y < -this.height) databus.removeBullets(this)
  25. }
  26. }