CameraControl.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Learn cc.Class:
  2. // - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
  4. // Learn Attribute:
  5. // - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. target: {
  14. default: null,
  15. type: cc.Node
  16. },
  17. lastUpdateTargerPosY:0
  18. },
  19. // use this for initialization
  20. onLoad: function () {
  21. this.camera = this.getComponent(cc.Camera);
  22. },
  23. onEnable: function () {
  24. // cc.director.getPhysicsManager().attachDebugDrawToCamera(this.camera);
  25. //cc.director.getCollisionManager().attachDebugDrawToCamera(this.camera);
  26. },
  27. onDisable: function () {
  28. // cc.director.getPhysicsManager().detachDebugDrawFromCamera(this.camera);
  29. //cc.director.getCollisionManager().detachDebugDrawFromCamera(this.camera);
  30. },
  31. resetData: function()
  32. {
  33. this.lastUpdateTargerPosY = 0;
  34. },
  35. // called every frame, uncomment this function to activate update callback
  36. lateUpdate: function (dt) {
  37. //let targetPos = this.target.convertToWorldSpaceAR(cc.Vec2.ZERO);
  38. //this.node.position = this.node.convertToNodeSpaceAR(targetPos);
  39. var isUp = true;
  40. if (this.target.position.y > 0)
  41. {
  42. if (this.target.position.y - this.lastUpdateTargerPosY > 0)
  43. {
  44. this.lastUpdateTargerPosY = this.target.position.y;
  45. }
  46. else
  47. {
  48. isUp = false;
  49. }
  50. }
  51. if (isUp && this.target.position.y > 0)
  52. {
  53. this.node.position = this.target.position;
  54. }
  55. // let ratio = targetPos.y / cc.winSize.height;
  56. // this.camera.zoomRatio = 1 + (0.5 - ratio) * 0.5;
  57. },
  58. });