Skip to content

Send Looping Danmaku

Description

This section will provide examples on how to send a looping danmaku. There are two different modes for looping facile danmaku.

Hint

  1. Implemented through setloop. In this mode, the danmaku will not participate in collision detection during the looping playback, except for the first time.
  2. Implemented recursively through the destroy hook. This method will allow the looping danmaku to participate in collision detection, but the motion time of the looping playback may be inconsistent.

Implementation via setloop()

Adding a global hook will affect all danmaku.

ts
const manager = create<string>({
  plugin: {
    $beforeMove(danmaku) {
      // Set loop
      danmaku.setloop();
    },

    $moved(danmaku) {
      // Stop loop playback after 3 looping
      if (danmaku.loops >= 3) {
        danmaku.unloop();
      }
    },
  },
});

By adding a plugin to the danmaku itself, you can make it effective for only a specific danmaku.

You can copy the following code and paste it into the console of the online demo to see the effect.

ts
manager.push('content', {
  plugin: {
    beforeMove(danmaku) {
      // Set loop
      danmaku.setloop();
    },

    moved(danmaku) {
      // Stop loop playback after 3 looping
      if (danmaku.loops >= 3) {
        danmaku.unloop();
      }
    },
  },
});

Implementing Loop Playback via Recursion

The above implementation leverages the official API, but you can also implement it recursively yourself.

Hint

Flexible danmaku will not participate in collision detection, so if you are dealing with flexible danmaku, do not use this method. Instead, use setloop.

You can copy the following code and paste it into the console of the online demo to see the effect.

ts
let loops = 0;

manager.push('content', {
  plugin: {
    destroyed(danmaku, mark) {
      // Stop loop playback after 3 looping
      if (++loops >= 3) return;

      // If you are triggering the hook by manually calling the destroy method
      // You can pass a mark via `danmaku.destroy('mark')` to make a judgment
      if (mark === 'mark') return;

      // If you have limits on memory and view, it may cause the send to fail
      // You can call `manager.canPush('facile')` to check
      manager.unshift(danmaku);
    },
  },
}),

Released under the MIT License.