runAction Won't work

I have a node that I want to moveTo a position that touched on its parent.

onLoad: function () {
        this.node.parent.on('touchstart', function ( event ) {
            console.log('Tourched!');
            var xpos = event.getLocationX();
            var ypos = event.getLocationY();

        var aksi = cc.moveTo(0.2,xpos,ypos);
        this.runAction(aksi);
        console.log(xpos);
        console.log(ypos);
        
    });
},

Everything above works but this.runAction I don’t see the problem can someone help.

Maybe i need to try to acces child and runAction ? if so how ?

Thanks.

try this.node.runAction(aksi);

tried that already it didn’t work, tried again and it gives this

Uncaught TypeError: Cannot read property ‘runAction’ of undefined

Can you try scheduling runAction() after 1 second(just for experiment, i have a doubt) and see what happens?

this.schedule(this.runAction(aksi), 1);
is this right ? if it is it gives

Sorry, cc.Node.schedule is removed.
Uncaught TypeError: this.schedule is not a function

this does the same
this.schedule(function() {this.runAction(aksi)}, 1);

You are using scheduler wrong, There is no scheduler attached with cc.Node, but attached with other components like cc.Sprite, cc.Component, cc.Label etc
http://cocos2d-x.org/docs/creator/manual/en/scripting/scheduler.html#caution-ccnode-does-not-contain-scheduler-api.
can also use setTimeout(()=>{
// runaction here
}, 1000)

Also you should try scheduling your runAction in start callback and see what happens
http://cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html#start

1 Like

when i runAction in this.node.on it runs and works but when inside this.node.parent.on it doesnt just fyi maybe it ll give you an idea

Replace 2nd last line of your code as posted in your question by
}.bind(this));
and see what happends?

this.runAction is not a function

Are you inheriting from component? Can you show me your whole instantiating code for this child node? Try this.node.runAction() or this.getComponent(cc.Node).runAction();

1 Like

Try this

1:
this.node.runAction(aksi)

2:
}. bind(this) at the end of function

1 Like

Whole Code:
onLoad: function () {
this.node.parent.on(‘touchstart’, function ( event ) {
console.log(‘Tourched!’);
var xpos = event.getLocationX();
var ypos = event.getLocationY();

    var aksi = cc.moveTo(0.2,xpos,ypos);
    this.node.runAction(aksi);
    console.log(xpos);
    console.log(ypos);
    
}.bind(this));

},

1 Like

Thanks guys @leonardowonder it worked can you explain how?

on these situations, a node can run action(objects that inherit cc.Node have a method called ‘runAction’):
1.node is in the render tree
2.node’s active is true

your code this.runAction, the ‘this’ in this line is a function, not a node, so this.runAction can’t work.
when a function{}.bind(this) , ‘this’ in the function is no longer the function, but the component,
so ‘this.node’ means the node that attached to this component. so this.node has the ‘runAction’ method.

by the way, if you don’t want to use ‘bind(this)’, you can also try this one:

onLoad: function () {
this.node.parent.on(‘touchstart’, ( event ) => {
console.log(‘Tourched!’);
var xpos = event.getLocationX();
var ypos = event.getLocationY();

var aksi = cc.moveTo(0.2,xpos,ypos);
this.node.runAction(aksi);
console.log(xpos);
console.log(ypos);

});
},

1 Like

Thanks so much :hugs:

While this is working for you, It might not work if you have more than one child attached to a parent node. Only last child which gets registered will work on touch. You are effectively replacing/overriding parents touchstart while instantiating your child node. This will also override any function bind to touchstart in parent script(If any). If you have multiple children or are planning for multiple children in future then consider moving above logic in parent node component script, locate each child by its id/name and move it accordingly.

1 Like