Shake.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. cc.Shake = cc.Class({
  2. name: "cc.Shake",
  3. extends:cc.ActionInterval,
  4. _initial_x: 0,
  5. _initial_y: 0,
  6. _strength_x: 0,
  7. _strength_y: 0,
  8. ctor(duration, strength_x, strength_y) {
  9. this.initWithDuration(duration, strength_x, strength_y);
  10. },
  11. initWithDuration(duration, strength_x, strength_y) {
  12. cc.ActionInterval.prototype.initWithDuration.call(this, duration)
  13. this._strength_x = strength_x;
  14. this._strength_y = strength_y;
  15. return true;
  16. },
  17. rangeRand(min, max) {
  18. let rnd = Math.random();
  19. return rnd * (max - min) + min;
  20. },
  21. update() {
  22. let randx = this.rangeRand(-this._strength_x, this._strength_x);
  23. let randy = this.rangeRand(-this._strength_y, this._strength_y);
  24. this.getTarget().setPosition(randx + this._initial_x, randy + this._initial_y);
  25. },
  26. startWithTarget(target) {
  27. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  28. this._initial_x = target.x;
  29. this._initial_y = target.y;
  30. },
  31. stop() {
  32. this.getTarget().setPosition(new cc.Vec2(this._initial_x, this._initial_y));
  33. cc.ActionInterval.prototype.stop.call(this);
  34. },
  35. });
  36. cc.shake = function (duration, strength_x, strength_y) {
  37. return new cc.Shake(duration, strength_x, strength_y);
  38. };