Skip to content

Commit e8b77a8

Browse files
committed
[#1091] new parentApp getter that returns the parent application/game instance to which a renderable belongs to
1 parent 2166c64 commit e8b77a8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [15.15.0] (melonJS 2) - _2023-11-20_
4+
5+
### Added
6+
- Renderable : new `parentApp` getter that returns the parent application/game instance to which a renderable belongs to.
7+
8+
39
## [15.14.0] (melonJS 2) - _2023-10-17_
410

511
### Added

src/renderable/renderable.js

+18
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,30 @@ export default class Renderable extends Rect {
258258
// viewport flag
259259
this._inViewport = false;
260260

261+
// cache value for the parentApp
262+
this._parentApp = undefined;
263+
261264
// renderable cache tint value used by the getter/setter
262265
this._tint = pool.pull("Color", 255, 255, 255, 1.0);
263266

264267
// ensure it's fully opaque by default
265268
this.setOpacity(1.0);
266269
}
267270

271+
/**
272+
* returns the parent application (or game) to which this renderable is attached to
273+
* @return {Application} the parent application or undefined if not attached to any container/app
274+
*/
275+
get parentApp() {
276+
if (typeof this._parentApp === "undefined") {
277+
if (typeof this.ancestor !== "undefined" && typeof this.ancestor.getRootAncestor === "function") {
278+
// the `app` property is only defined in the world "root" container
279+
this._parentApp = this.ancestor.getRootAncestor().app;
280+
}
281+
}
282+
return this._parentApp;
283+
}
284+
268285
/**
269286
* Whether the renderable object is floating (i.e. used screen coordinates), or contained in a floating parent container
270287
* @see Renderable#floating
@@ -790,6 +807,7 @@ export default class Renderable extends Rect {
790807
}
791808

792809
this.ancestor = undefined;
810+
this._parentApp = undefined;
793811

794812
// destroy the physic body if defined and is a builtin body object
795813
if ((typeof this.body !== "undefined") && (typeof this.body.destroy === "function")) {

0 commit comments

Comments
 (0)