FStoreTS.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { SpriteActionType } from "../object/FSprite";
  2. import BaseEvent from "./base/BaseEvent";
  3. const {ccclass, property} = cc._decorator;
  4. /**
  5. * 推石头
  6. *
  7. * tag == 1 左右推动
  8. * tag == 2 上下推动
  9. */
  10. @ccclass
  11. export default class FStoreTS extends BaseEvent {
  12. @property({
  13. displayName: '提示按钮图标',
  14. type: cc.SpriteFrame
  15. })
  16. mTipsIcon: cc.SpriteFrame = null;
  17. private mRigidBody:cc.RigidBody = null
  18. /**
  19. * 推动中
  20. */
  21. private isPull = false;
  22. /**
  23. * 当前移动方向
  24. */
  25. private moveVt = cc.v2()
  26. start(){
  27. this.mRigidBody = this.node.getComponent(cc.RigidBody);
  28. }
  29. onBegin(tag:number){
  30. this.startEvent()
  31. if(tag == 1){
  32. this.moveVt.y = 0
  33. let myX = this.ff.mainSprite.node.x
  34. if(myX < this.node.x){
  35. this.moveVt.x = 7
  36. }else{
  37. this.moveVt.x = -7
  38. }
  39. }else if(tag == 2){
  40. this.moveVt.x = 0
  41. let myY = this.ff.mainSprite.node.y
  42. if(myY < this.node.y){
  43. this.moveVt.y = 7
  44. }else{
  45. this.moveVt.y = -7
  46. }
  47. }
  48. }
  49. onEnd(tag:number){
  50. this.endEvent()
  51. }
  52. private startEvent(){
  53. let fControl = this.ff.control;
  54. fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_START, this._touchRunningStartEvent, this);
  55. fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchRunningMoveEvent, this);
  56. fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_END, this._touchRunningEndEvent, this);
  57. fControl.mEventButton.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchRunningEndEvent, this);
  58. this.showOpt(this.mTipsIcon, () => {
  59. })
  60. }
  61. private endEvent(){
  62. this.closeOpt()
  63. let fControl = this.ff.control;
  64. fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_START);
  65. fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_MOVE);
  66. fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_END);
  67. fControl.mEventButton.node.off(cc.Node.EventType.TOUCH_CANCEL);
  68. this.isPull = false;
  69. }
  70. private _touchRunningMoveEvent(){
  71. cc.log('_touchRunningMoveEvent')
  72. }
  73. private _touchRunningStartEvent(){
  74. cc.log('_touchRunningStartEvent')
  75. this.isPull = true;
  76. cc.tween(this).sequence(
  77. cc.delayTime(0.01),
  78. cc.callFunc(()=>{
  79. this.mRigidBody.type = cc.RigidBodyType.Dynamic
  80. })
  81. ).start()
  82. let mainSprite = this.ff.mainSprite
  83. // mainSprite.spine.setAnimation(0, SpriteActionType.run, true);
  84. }
  85. private _touchRunningEndEvent(){
  86. cc.log('_touchRunningEndEvent')
  87. this.isPull = false;
  88. cc.tween(this).sequence(
  89. cc.delayTime(0.01),
  90. cc.callFunc(()=>{
  91. this.mRigidBody.type = cc.RigidBodyType.Static
  92. })
  93. ).start()
  94. // let mainSprite = this.ff.mainSprite
  95. // mainSprite.spine.setAnimation(0, SpriteActionType.stand, true);
  96. }
  97. public update(dt){
  98. if(this.isPull){
  99. let mainSprite = this.ff.mainSprite
  100. mainSprite.node.x += this.moveVt.x
  101. mainSprite.node.y += this.moveVt.y
  102. }
  103. }
  104. }