1
var $jscomp = $jscomp || {};
$jscomp.scope = {};
$jscomp.ASSUME_ES5 = !1;
$jscomp.ASSUME_NO_NATIVE_MAP = !1;
$jscomp.ASSUME_NO_NATIVE_SET = !1;
$jscomp.SIMPLE_FROUND_POLYFILL = !1;
$jscomp.objectCreate =
  $jscomp.ASSUME_ES5 || "function" == typeof Object.create
    ? Object.create
    : function (e) {
        var m = function () {};
        m.prototype = e;
        return new m();
      };
$jscomp.underscoreProtoCanBeSet = function () {
  var e = { a: !0 },
    m = {};
  try {
    return (m.__proto__ = e), m.a;
  } catch (w) {}
  return !1;
};
$jscomp.setPrototypeOf =
  "function" == typeof Object.setPrototypeOf
    ? Object.setPrototypeOf
    : $jscomp.underscoreProtoCanBeSet()
    ? function (e, m) {
        e.__proto__ = m;
        if (e.__proto__ !== m) throw new TypeError(e + " is not extensible");
        return e;
      }
    : null;
$jscomp.inherits = function (e, m) {
  e.prototype = $jscomp.objectCreate(m.prototype);
  e.prototype.constructor = e;
  if ($jscomp.setPrototypeOf) {
    var w = $jscomp.setPrototypeOf;
    w(e, m);
  } else
    for (w in m)
      if ("prototype" != w)
        if (Object.defineProperties) {
          var B = Object.getOwnPropertyDescriptor(m, w);
          B && Object.defineProperty(e, w, B);
        } else e[w] = m[w];
  e.superClass_ = m.prototype;
};

var Game = function () {
  return Phaser.Scene.call(this, "game") || this;
};
$jscomp.inherits(Game, Phaser.Scene);
Game.prototype.create = function () {
  this.someFunctionName = fn;
  function fn(a) {
    dosomething...
  }
}

var config = {
    type: Phaser.WEBGL,
    transparent: !0,
    width: 720,
    height: 1080,
    scale: {
      mode: Phaser.Scale.FIT,
      parent: "game_content",
      autoCenter: Phaser.Scale.CENTER_BOTH,
    },
    scene: [Boot, Load, Menu, Level, Game],
  },
game = new Phaser.Game(config);

game.create();
console.log(game.someFunctionName());

So how to access the function fn? So how to access the function fn?

Can't access the function fn! Got this error "caught TypeError: game.create is not a function". I just want to use the fn function outsider.

Can't access the function fn! Got this error "caught TypeError: game.create is not a function". I just want to use the fn function outsider.

uronpen
  • 13
  • 3
  • 1
    How and Why do you want to want to access this function? is the function ***static*** or not? basically without changing the code you can't call the function, that's why more context, on how you want to use it, is needed. – winner_joiner Apr 23 '23 at 10:40

1 Answers1

0

Depending on your usecase you could simply attach the function to the current instance:

...
var Game = function () {
    return Phaser.Scene.call(this, "game") || this;
};

$jscomp.inherits(Game, Phaser.Scene);

Game.prototype.create = function () {
 
    function fn(a){
        ...
    }

    this.someFunctionName = fn; // <-- an easy solution, works in some cases
}

And than you can access the function through the someFunctionName method. (but you will need to call the create function on the instance, so that the function is bound to the instance)

I removed the alternative Version, since after thinking about it it is a very bad way to solve this problem

UPDATE:
Disclaimer: This should work, but I don't know why you would ever want to do this.

...
var game = new Phaser.Game(config);

// the game is not created right await you need to wait, abit.
setTimeout( ( ) => {
    console.info(game.scene.keys['game'].someFunctionName())
}, 0);
...

Info: the key game is due to the parameter passed, to this line of code return Phaser.Scene.call(this, "game") || this;. If this value would change you would have to adapt it, in both places.

personally I would use ES6 inheritance to create Custom Scene this makes everything more readable. Checkout this example

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • Got "undefined" console message by using Game.prototype.someFunctionName. @winner_joiner – uronpen Apr 24 '23 at 01:38
  • You would have to call the `someFunctionName` on an instance, so with other Words `var test = Game(); test.someFunctionName();`, but there are other ways I updated an alternative. – winner_joiner Apr 24 '23 at 05:11
  • Got this console message "Game.someFunctionName is not a function"@winner_joiner – uronpen Apr 24 '23 at 08:09
  • Sorry the alternative is a possible solution, but it is not really good. Since it still only will work if the `create` function is called atleast once. Why don't you just uset the first version. You just need to call `create` on the instance before calling the `someFunctionName`. `var test = Game(); ...; test.create(); ....; test.someFunctionName();` – winner_joiner Apr 24 '23 at 08:12
  • The first version got the error message "test.create is not a function". – uronpen Apr 24 '23 at 09:57
  • I don't know `jscomp.inherits` does, your code ist not really clear too me. if `jscomp`is extending a class the creating of test should look like this `var test = new Game()`, If using the new keyword doesn't solve your problem it might be better to share more code explaining, what you want to achieve. ***btw.:** You can update your question, you don't need to post code in an answer.* – winner_joiner Apr 24 '23 at 10:24
  • I update the code. Got an error too. – uronpen Apr 24 '23 at 11:24
  • @uronpen I updated my answer with a working solution. – winner_joiner Apr 24 '23 at 12:12
  • @uronpen no problem, if it solved your question, please consider accepting my answer with the green checkmark. :) – winner_joiner Apr 24 '23 at 13:31