Change the radius of a circle collider through script

Change the radius of a circle collider through script
0.0 0

#1

I need to make a dynamic circle collider component for a node.

the prefab i am adding the collider to does not have one already set in cocos creator.

i have tried this…

var Collider = new(cc.Collider.Circle);
Collider.Radius = bombRadius;
this.node.addComponent(Collider);

I get this error;
Error 3809, please go to https://github.com/cocos-creator/engine/blob/master/EngineErrorMap.md#3809 to see details.

error 3809
addComponent: The component to add must be a constructor

I thought the better way would be to set a circle collider for the prefab in the cocos creator UI and then just update it through the script on initialization of the prefab however i can’t find the setCircleColliderRadius() function.

Does any one know how i can achieve either of these methods? hopefully both :wink:


#2

You do it in reverse order. addComponent takes a type and ultimately constructs your object for you. Try this instead

cc.director.getCollisionManager().enabledDebugDraw = true;
cc.director.getCollisionManager().enabled = true;
this.node.addComponent(cc.CircleCollider);
this.node.getComponent(cc.CircleCollider).radius = this.node.width/2;
this.node.getComponent(cc.CircleCollider).enabled = true;

#3

Thank you very much kind sir :slight_smile:


#4

Good Help.