Vine.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Rope Vine
  2. var NUM_OBJECTS = 10;
  3. var node = self.node;
  4. var x = node.position2D[0];
  5. var y = node.position2D[1];
  6. node.position = [0, 0, 0];
  7. function start() {
  8. // create the node body
  9. var groundBody = node.createComponent("RigidBody2D");
  10. var prevBody = groundBody;
  11. for (var i = 0; i < NUM_OBJECTS; i++) {
  12. // can we create off our node instead of scene?
  13. var vnode = scene.createChild("RigidBody");
  14. var sprite2D = vnode.createComponent("StaticSprite2D");
  15. sprite2D.sprite = cache.getResource("Sprite2D", "Sprites/vine.png");
  16. var vbody = vnode.createComponent("RigidBody2D");
  17. vbody.bodyType = Atomic.BT_DYNAMIC;
  18. // Create box
  19. var vbox = vnode.createComponent("CollisionBox2D");
  20. // Set friction
  21. vbox.friction = .2;
  22. // Set mask bits.
  23. vbox.maskBits = 0xFFFF & ~0x0002;
  24. vnode.position = [x + 0.5 + 1.0 * i, y, 0.0];
  25. vbox.size = [1.0, 0.1];
  26. vbox.density = 5.0;
  27. vbox.categoryBits = 0x0001;
  28. if (i == NUM_OBJECTS - 1)
  29. vbody.angularDamping = 0.4;
  30. var joint = vnode.createComponent("ConstraintRevolute2D");
  31. joint.otherBody = prevBody;
  32. joint.anchor = [x + i, y];
  33. joint.collideConnected = false;
  34. prevBody = vbody;
  35. }
  36. var constraintRope = node.createComponent("ConstraintRope2D");
  37. constraintRope.otherBody = prevBody;
  38. constraintRope.ownerBodyAnchor = [x, y];
  39. constraintRope.maxLength = (NUM_OBJECTS + 0.01);
  40. }