How do I call an action from a behavior?

For developers using the Construct 2 Javascript SDK

Post » Tue Jul 29, 2014 12:18 am

I'm trying to write a behavior that will change the character scale of a sprite font. However, I'm having trouble getting it to work. After digging around in the forum, I came up with this:

Code: Select all
cr.plugins_.Spritefont2.prototype.acts.SetScale.call(this.inst, 0.5);

That at least doesn't throw any errors, but it also doesn't actually change the scale.

Does anyone know what I'm doing wrong?

Thanks. :)
B
8
S
2
Posts: 55
Reputation: 616

Post » Tue Jul 29, 2014 12:50 am

OK, in the docs I found this:

inst.inst
In behavior instances only: this is the reference to the object instance your behavior should modify.

Which sounds like it would apply to what I'm trying to do. So I amend my code to:

Code: Select all
cr.plugins_.Spritefont2.prototype.acts.SetScale.call(this.inst.inst, 0.5);

But that gives me:

Uncaught TypeError: Cannot read property 'characterScale' of undefined

Still searching...
B
8
S
2
Posts: 55
Reputation: 616

Post » Tue Jul 29, 2014 3:22 am

@TrickyWidget

It is possible to call an action from a behavior. Even call an action/expression/condition from other plugin/behavior.
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679

Post » Tue Jul 29, 2014 5:14 am

rex is definitely the authority on plugins but to add to what i think your problem is that you are not passing the correct instance of the spritefont object your trying to change to the function you are calling.

"this.inst" <- I assume that's the instance of your plugin your passing to the function call? But what it needs to be is the sprite font object instance you want to scale.

You need to somewhere get the instance of the sprite font object usually by adding a condition to pick it. Once picked you store instance of the sprite font into a variable like "this.spriteFontInstance"

Your function call would then look like this.
Code: Select all
cr.plugins_.Spritefont2.prototype.acts.SetScale.call(this.spriteFontInstance, 0.5);
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Tue Jul 29, 2014 5:35 am

Or run the code step by step by adding
Code: Select all
debugger;

before the call, to see if the action has been called or not.
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679

Post » Tue Jul 29, 2014 11:22 am

rexrainbow wrote:It is possible to call an action from a behavior. Even call an action/expression/condition from other plugin/behavior.


Thank you for the response! It was actually that very code that I examined from a previous post that lead me to the code I had.

It seems my problem was that I was trying to affect the instance in behinstProto.onCreate. I have no idea why that doesn't work, but when I move it down into behinstProto.tick it's now behaving as expected. :)

Thanks again!
B
8
S
2
Posts: 55
Reputation: 616

Post » Tue Jul 29, 2014 1:16 pm

I guess that , the this.inst had not been created at behavior's on create function. I had this problem before, too.
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests