How does intanceProto work?

0 favourites
  • 4 posts
  • Hi

    I have problems understanding what javascript is doing.

    I have this code:

    <font face="Courier New, Courier, mono">

    Acts.prototype.MakeDungeon = function (text_value, mapSizeX, mapSizeY)

         {       

              this.testFunction(2,2); //This works

                  Acts.prototype.MakeCustomDungeon (text_value, mapSizeX, mapSizeY, 5, 15, 150, 50);

         };

         

         Acts.prototype.MakeCustomDungeon = function (text_value, mapSizeX, mapSizeY, roomMinSize, roomMaxSize, roomMaxArea, roomsMaxNum )

         {       

              this.testFunction(2,2); //This DOESN?T works

         };

         

         instanceProto.testFunction = function (x,y)

            {

                     //do stuff

            }</font>

    I have two Acts that can be called from the editor. One has more options than the other, so when I call <font face="Courier New, Courier, mono">MakeDungeon</font>, it itself calls <font face="Courier New, Courier, mono">MakeCustomDungeon</font> and gives default values to the missing information.

    The thing is that when I call <font face="Courier New, Courier, mono">this.testFunction</font>, it works only from the <font face="Courier New, Courier, mono">MakeDungeon</font>, and not the <font face="Courier New, Courier, mono">MakeCustomDungeon</font>.

    When the <font face="Courier New, Courier, mono">this.testFunction</font> is called from <font face="Courier New, Courier, mono">MakeCustomDungeon</font> (via <font face="Courier New, Courier, mono">MakeDungeon</font>), I get an error message:

    <font face="Courier New, Courier, mono">Javascript error! TypeError: this.testFunction is not a function</font>

    Can someone help me figure out what?s going on?

  • If you call Acts.prototype.MakeCustomDungeon directly, it will pass the global context for 'this'. Try using call() instead.

    Actually, in general I would recommend you do not make actions which call other actions directly; they should both just call a common function.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks. But when you say:

    Actually, in general I would recommend you do not make actions which call other actions directly; they should both just call a common function.

    how should I write the common function?

    <font face="Courier New, Courier, mono">instanceProto.constructorFunction = function (param1,param2)</font>

    or

    <font face="Courier New, Courier, mono">function constructorFunction (param1,param2)</font>

    or something different (I?m having a lot of trouble figuring this thins out)

  • I meant both actions should just call testFunction, instead of calling an action which then calls testFunction.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)