Javascript syntax

As a Unity3D dev, I am new to PlayCanvas and Javascript
I am using Tween lib, and somehow I can’t call a function inside on. body
I can only use global variables, am I doing something wrong here?

Hi @Dava,

You can easily call methods from your original script by specifying the correct context. There are several ways in doing that:

.tween(entity.getLocalPosition()).to({x: 10, y: 0, z: 0}, 1.0, pc.SineOut)
.on('complete', function(){
   // now you can call methods from your script

or a simpler way:

var self = this;

.tween(entity.getLocalPosition()).to({x: 10, y: 0, z: 0}, 1.0, pc.SineOut)
.on('complete', function(){
   // now you can call methods from your script
1 Like

I already tried , but it’s not working

Check my example code on how I append }.bind(this) at the end of the complete callback.

1 Like

It’s working now, thank you @Leonidas

One more question, I am trying to simulate coroutine, is this possible with tween lib?

Right, yes you can use a generic app tween and the complete event for that:{value: 0}).to({value: 1}, 3.0, pc.BackOut)
.on('complete', this.myScriptMethod.bind(this))

Edit: removed delay, no need for that just set the desired duration as delay.

Right, the reason for that error is because you call the method passing an argument before bind is applied. The syntax I proposed would work only this way, with no arguments:


In your case to pass the argument and have bind set the context simple do:


Thank you , this was helpful :smiley:

1 Like

Can I bind a function somehow as you did there, to this method, so I don’t need to use the global variable and update function? @Leonidas

Hi @Dava,

Yes you can easily do that with any function:

var myCustomCallback = function(){
   // now this has access to all member properties of the script object it's being executed in

Specifically for PlayCanvas related events you can do it in another way by specifying the context as the 3rd parameter in the event. In your example:'load:'+, function(asset){

   // now you have access to the script context
   var myEntity ='test');

}, this);

It’s working now , thank you :slight_smile:

One more thing , i lost intellisense in all of my scripts , is there any specific reason for that ?

If it stopped everywhere, maybe it’s related to this:

1 Like

Does anyone know why string.length doesn’t return anything ?

var day=d.getDate();
if(day.length ==1)

Hi Dava. day is a number,

You could try if (day < 10 ) dayString = '0' + day

1 Like

Hi @Leonidas
How can I find an index of the child element?
I would have many elements, so foreach is too much for this task, does PC have a function like in Unity ?

Hi @Dava ,

No there isn’t, so you will have to either use forEach or even better findIndex (ES6) JS methods.

Or keep track of the indexes for each entity on another list of yours when creating them.

1 Like