bullet.js 656 B

123456789101112131415161718192021222324252627282930313233343536
  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. let 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 )
  25. databus.removeBullets(this)
  26. }
  27. }