camera-control.js 890 B

1234567891011121314151617181920212223242526272829303132
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. target: {
  5. default: null,
  6. type: cc.Node
  7. }
  8. },
  9. // use this for initialization
  10. onLoad: function () {
  11. this.camera = this.getComponent(cc.Camera);
  12. },
  13. onEnable: function () {
  14. cc.director.getPhysicsManager().attachDebugDrawToCamera(this.camera);
  15. },
  16. onDisable: function () {
  17. cc.director.getPhysicsManager().detachDebugDrawFromCamera(this.camera);
  18. },
  19. // called every frame, uncomment this function to activate update callback
  20. lateUpdate: function (dt) {
  21. let targetPos = this.target.convertToWorldSpaceAR(cc.Vec2.ZERO);
  22. this.node.position = this.node.parent.convertToNodeSpaceAR(targetPos);
  23. let ratio = targetPos.y / cc.winSize.height;
  24. this.camera.zoomRatio = 1 + (0.5 - ratio) * 0.5;
  25. },
  26. });