How to apply linear Velocity?

How to apply linear Velocity?

 window.rb = this.node.getComponent(cc.RigidBody);
window.rb.linearVelocity = 100;

linearVelocity applied to rigidBody at start and it works but

window.rb.linearVelocity = 100;

in above function linearVelocity does not work at all, as I want to apply continuous linear velocity on RigidBody also other properties like window.rb.sensor = true not even work in above function

I’m new to Cocos2D-x can someone solve this simple issue?



@ousaf You can use the following code:

this.rigidBody.linearVelocity = new cc.Vec2(this.direction * this.speed * deltaTime, this.rigidBody.linearVelocity.y);

this will create a linear velocity in x axis. set direction to 1 or -1 accordingly.


@DanielMatute has a good solution. Thanks Daniel!!


Thanks @DanielMatute your solution works…just curious to know that why

this.rigidBody.linearVelocity.x = this.direction * this.speed * deltaTime;
not works in update(dt){} function but Works in onLoad(){} function

and why
this.physicsCircleCollider.sensor = true;
not works in update(dt){} function as it works in onload(){} function


@ousaf did you call scheduleUpdate()?


@DanielMatute IDK about that, is it like setInterval() function?
I have done the following way but it not still works…

onLoad () {
this.schedule(function() {
this.rigidBody.linearVelocity.x = 1000;
}, 16/1000);


@ousaf just call it this.scheduleUpdate(); when the game is initialized;

then work on your update function:

//Apply your velocity