Start/stop excalibur context

23 views
Skip to first unread message

Chklag

unread,
Nov 1, 2018, 8:21:01 AM11/1/18
to excaliburjs
I want to stop excalibur context to integrate it into an angular application. To do it i need to :
- Remove "Start app" div
- Remove events listeners

So to do it i've done : 

  public start() {
    this.oldEventListenerWindow = window.addEventListener;
    this.oldEventListenerDocument = document.addEventListener;
    const _this = this;
    window.addEventListener = function (...args: any[]) {
      _this.eventsAddedOnWindow.push({type: args[0], callback: args[1]});
      _this.oldEventListenerWindow.apply(this, args);
    };
    document.addEventListener = function (...args: any[]) {
      _this.eventsAddedOnDocument.push({type: args[0], callback: args[1]});
      _this.oldEventListenerDocument.apply(this, args);
    };
    this.game.start();
  }


  public stop(): void {
    this.game.stop();
    window.addEventListener = this.oldEventListenerWindow;
    document.addEventListener = this.oldEventListenerDocument;
    this.eventsAddedOnWindow.forEach((e) => {
      window.removeEventListener(e.type, e.callback);
    });
    this.eventsAddedOnDocument.forEach((e) => {
      document.removeEventListener(e.type, e.callback);
    });

    const elementButton = document.getElementById('excalibur-play');
    if (elementButton) {
      elementButton.parentElement.remove();
    }
  }

Do you have a better way to do it?

Erik Onarheim

unread,
Nov 1, 2018, 9:57:14 AM11/1/18
to Chklag, excaliburjs
Hello,

It is possible to suppress the play button in the excalibur constructor with this IEngineOption, you may need to unlock audio yourself (Chrome 70 change https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio )


```
new ex.Engine({
  suppressPlayButton: true
})
```

You may also want to set the pointer scope to canvas if pointer events are a problem https://excaliburjs.com/docs/api/edge/enums/_input_pointer_.pointerscope.html#canvas 

```
new ex.Engine({
   pointerScope: ex.Input.PointerScope.Canvas
})
```

As for removing event listeners on stop, I don't think excalibur does that cleanup. I've added an issue for the next release https://github.com/excaliburjs/Excalibur/issues/1063 

Let me know if you have any questions,
Erik


--
You received this message because you are subscribed to the Google Groups "excaliburjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to excaliburjs...@googlegroups.com.
To post to this group, send email to excal...@googlegroups.com.
Visit this group at https://groups.google.com/group/excaliburjs.
To view this discussion on the web visit https://groups.google.com/d/msgid/excaliburjs/d1791dbc-aa81-4181-b6c1-3778c571ac51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Chklag

unread,
Nov 1, 2018, 4:25:56 PM11/1/18
to excaliburjs
Thanks :)

Reply all
Reply to author
Forward
0 new messages