How does intanceProto work?

For developers using the Construct 2 Javascript SDK

Post » Fri Jun 21, 2013 9:55 pm

Hi

I have problems understanding what javascript is doing.

I have this code:


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 DOESNT works
     };
     
     instanceProto.testFunction = function (x,y)
        {
                 //do stuff
        }


I have two Acts that can be called from the editor. One has more options than the other, so when I call MakeDungeon, it itself calls MakeCustomDungeon and gives default values to the missing information.

The thing is that when I call this.testFunction, it works only from the MakeDungeon, and not the MakeCustomDungeon.

When the this.testFunction is called from MakeCustomDungeon (via MakeDungeon), I get an error message:

Javascript error! TypeError: this.testFunction is not a function

Can someone help me figure out whats going on?perezbalen2013-06-21 21:59:04
B
7
S
3
G
3
Posts: 53
Reputation: 2,459

Post » Fri Jun 21, 2013 11:06 pm

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.Ashley2013-06-21 23:07:10
Scirra Founder
B
378
S
220
G
84
Posts: 23,871
Reputation: 188,157

Post » Fri Jun 21, 2013 11:25 pm

Thanks. But when you say:
[QUOTE] Actually, in general I would recommend you do not make actions which call other actions directly; they should both just call a common function. [/QUOTE]
how should I write the common function?
instanceProto.constructorFunction = function (param1,param2)
or
function constructorFunction (param1,param2)
or something different (Im having a lot of trouble figuring this thins out)
perezbalen2013-06-21 23:26:27
B
7
S
3
G
3
Posts: 53
Reputation: 2,459

Post » Sat Jun 22, 2013 12:54 pm

I meant both actions should just call testFunction, instead of calling an action which then calls testFunction.
Scirra Founder
B
378
S
220
G
84
Posts: 23,871
Reputation: 188,157


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests