diff --git a/CHANGELOG.md b/CHANGELOG.md index e3ae09735..57f6b8e42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [15.15.0] (melonJS 2) - _2023-11-20_ + +### Added +- Renderable : new `parentApp` getter that returns the parent application/game instance to which a renderable belongs to. + + ## [15.14.0] (melonJS 2) - _2023-10-17_ ### Added diff --git a/src/renderable/renderable.js b/src/renderable/renderable.js index 6e2c7fdda..1e036984d 100755 --- a/src/renderable/renderable.js +++ b/src/renderable/renderable.js @@ -258,6 +258,9 @@ export default class Renderable extends Rect { // viewport flag this._inViewport = false; + // cache value for the parentApp + this._parentApp = undefined; + // renderable cache tint value used by the getter/setter this._tint = pool.pull("Color", 255, 255, 255, 1.0); @@ -265,6 +268,20 @@ export default class Renderable extends Rect { this.setOpacity(1.0); } + /** + * returns the parent application (or game) to which this renderable is attached to + * @return {Application} the parent application or undefined if not attached to any container/app + */ + get parentApp() { + if (typeof this._parentApp === "undefined") { + if (typeof this.ancestor !== "undefined" && typeof this.ancestor.getRootAncestor === "function") { + // the `app` property is only defined in the world "root" container + this._parentApp = this.ancestor.getRootAncestor().app; + } + } + return this._parentApp; + } + /** * Whether the renderable object is floating (i.e. used screen coordinates), or contained in a floating parent container * @see Renderable#floating @@ -790,6 +807,7 @@ export default class Renderable extends Rect { } this.ancestor = undefined; + this._parentApp = undefined; // destroy the physic body if defined and is a builtin body object if ((typeof this.body !== "undefined") && (typeof this.body.destroy === "function")) {