Skip to content

Commit

Permalink
[#1091] new parentApp getter that returns the parent application/ga…
Browse files Browse the repository at this point in the history
…me instance to which a renderable belongs to
  • Loading branch information
obiot committed Nov 20, 2023
1 parent 2166c64 commit e8b77a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/renderable/renderable.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,30 @@ 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);

// ensure it's fully opaque by default
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
Expand Down Expand Up @@ -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")) {
Expand Down

0 comments on commit e8b77a8

Please # to comment.