Error on cc.Node.setPosition

In my script, I want to teleport a node somewhere else when a collision occurs:

onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
    otherCollider.node.position = cc.v2(0, 0);
}

Seems straightforward enough, however this error occurs:

Simulator: ERROR: Uncaught Error, location: src/cocos2d-jsb.js:0:0
STACK:
[0]321.b2Body.SetTransformXY@src/cocos2d-jsb.js:71335
[1]321.b2Body.SetTransformVec@src/cocos2d-jsb.js:71332
[2]syncPosition@src/cocos2d-jsb.js:25269
[3]_onNodePositionChanged@src/cocos2d-jsb.js:25309
[4]190.CallbacksInvoker.invoke@src/cocos2d-jsb.js:29910
[5]emit@src/cocos2d-jsb.js:9460
[6]setPosition@src/cocos2d-jsb.js:7694
[7]Tunnel.onBeginContact@assets/game/tunnel/Tunnel.js:45
[8]149.PhysicsContact.emit@src/cocos2d-jsb.js:24371
[9]_onBeginContact@src/cocos2d-jsb.js:24679
[10]171.PhysicsContactListener.BeginContact@src/cocos2d-jsb.js:26505
[11]321.b2Contact.Update@src/cocos2d-jsb.js:75138
[12]321.b2ContactManager.Collide@src/cocos2d-jsb.js:75566
[13]321.b2World.Step@src/cocos2d-jsb.js:80093
[14]update@src/cocos2d-jsb.js:24498
[15]update@src/cocos2d-jsb.js:10390
[16]mainLoop@src/cocos2d-jsb.js:8265
[17]callback@src/cocos2d-jsb.js:8493
[18]tick@jsb-adapter/jsb-builtin.js:2001
[ERROR] (/Users/nantas/fireball-x/cocos2d-x-lite_21/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp, 521): Invoking function (0x7feac345a430) failed!
    at HTMLElement.print-simulator-log (/Volumes/FastExternal/Applications/CocosCreator2.1.0.app/Contents/Resources/app.asar/editor/builtin/scene/panel/messages/scene.js:1:1653)
    at Object.e._dispatch (/Volumes/FastExternal/Applications/CocosCreator2.1.0.app/Contents/Resources/app.asar/editor-framework/lib/renderer/panel.js:1:1941)
    at EventEmitter.o.on.s (/Volumes/FastExternal/Applications/CocosCreator2.1.0.app/Contents/Resources/app.asar/editor-framework/lib/renderer/ipc.js:1:2917)
    at emitMany (events.js:127:13)
    at EventEmitter.emit (events.js:204:7)

it also happens when I’m using node.setPosition instead of assigning to the position property.

I’m on Cocos Creator 2.1.0.

Turns out you can’t perform changes to the world in a collision handler, as the Box2D world is locked at that point. So my solution was to schedule the moving of the node to be performed the next time update is called.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.