123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- /**
- * 绘制钓鱼线,线在水里飘的效果
- */
- cc.Class({
- extends: cc.Component,
- properties: {
-
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- Module.goFishingObj = this;
- this.touchEnbled = true;
- this.touchId = [];
- Module.state = true;
- this.moveCount = 0;
- this.isShouGou = true
- },
- start () {
- this.init();
- },
- /**
- * 初始化鱼钩
- */
- init: function() {
- this.windowSize=cc.view.getVisibleSize();
- this.y1 = this.windowSize.height + 300;
- this.x1 = this.windowSize.width / 2;
-
- this.y2 = this.windowSize.height - this.windowSize.height/ 2 / 2 ;
- this.x2 = this.windowSize.width / 2;
- this.y3 = this.windowSize.height / 2 -50;
- this.x3 = this.windowSize.width / 2;
- this.hook = this.node.getChildByName('hook');
- this.hook.x = this.x3 - 14;
- this.hook.y = this.y3 + 3;
- this.limits = this.node.getChildByName('limits');
- this.limits.x = this.x3 - 14;
- this.limits.y = this.y3 + 3;
- var ctx = this.getComponent(cc.Graphics);
- if (!ctx) ctx = this.addComponent(cc.Graphics);
-
- ctx.clear();
- ctx.lineWidth = 2;
- ctx.strokeColor = cc.Color.BLACK;
- ctx.moveTo(this.x1, this.y1);
- ctx.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
- ctx.stroke();
-
- Module.fishingLine = this.fishingLine = ctx;
-
- this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegin.bind(this), this );
- this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoving.bind(this), this );
- this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd.bind(this), this );
- this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel.bind(this), this );
- },
- /**
- * 触摸开始
- */
- touchBegin:function(touch,event){
- if (this.touchId.length != 0) return;
- this.touchId.push(touch.getID());
- this.lastTouch = touch.getLocation();
- },
- /**
- * 移动中
- */
- touchMoving:function(touch,event){
- if (Module.isShouGou) return;
- var width = this.windowSize.width;
- if (touch.getID() != this.touchId[0]) return;
- if (touch.getLocation() == undefined || this.lastTouch == undefined) return;
- this.isShouGou = true;
- var p = touch.getLocation();
- var movex = p.x - this.lastTouch.x;
- var movey = p.y - this.lastTouch.y;
- if(this.x3 >= 0 && this.x3 <= width){
- var len = (p.x-this.lastTouch.x)*(p.x-this.lastTouch.x)+(p.y-this.lastTouch.y)*(p.y-this.lastTouch.y)
- var length = Math.pow(len,2);
- if(length > 1){
- this.x3+= movex * 1.2;
- this.lastTouch = p;
- }
- }
- if(this.x3 < 0){ this.x3 = 0 }
- if(this.x3 > this.windowSize.width ){ this.x3 = this.windowSize.width }
-
- if (this.fishingLine) {
- this.fishingLine.clear();
- this.fishingLine.moveTo(this.x1, this.y1);
- this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
- this.fishingLine.stroke();
- Module.fishingLine = this.fishingLine;
- Module.state = true;
- this.moveCount = 0;
- this.hook.x = this.x3- 14;
- this.limits.x = this.hook.x;
- this.setHookRotation();
- }
- },
- /**
- * 设置鱼钩角度
- */
- setHookRotation: function() {
- var r = 0;
- if (this.hook.x > this.windowSize.width / 2) {
- r = -((this.hook.x - this.windowSize.width / 2)/10 / 2);
- } else {
- r = Math.abs((this.hook.x- 320))/10 /2;
- }
- this.limits.x = this.hook.x;
- this.hook.rotation = r;
- },
- /**
- * 触摸结束
- */
- touchEnd:function(touch, event){
- if (touch.getID() == this.touchId[0]) {
- this.touchId = [];
- }
- },
- /**
- * 触摸取消
- */
- touchCancel: function(touch, event){
- this.touchId = [];
- if (!Module.state) return;
- },
- /**
- * 钓鱼线摆动方向
- */
- update (dt) {
- //收钩
- if (Module.isShouGou && this.isShouGou) {
- if (this.x2 > this.windowSize.width / 2) {
- this.x2 -= 2;
- } else {
- this.x2 += 2;
- }
- var flag = 0;
- if (this.x3 > this.windowSize.width / 2) {
- this.x3 -= 2;
- flag = 1;
- } else {
- this.x3 += 2;
- flag = 2;
- }
- // if () {
- // }
- if (this.x3 > this.windowSize.width / 2 && flag == 2) {
- this.isShouGou = false;
- this.hook.rotation = 0;
- } else if (this.x3 < this.windowSize.width / 2 && flag == 1){
- this.isShouGou = false;
- this.hook.rotation = 0;
- }
- if (this.fishingLine) {
- this.fishingLine.clear();
- this.fishingLine.moveTo(this.x1, this.y1);
- this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
- this.fishingLine.stroke();
- Module.fishingLine = this.fishingLine;
- this.hook.x = this.x3- 14;
- this.limits.x = this.hook.x;
- this.setHookRotation();
- }
- return;
- }
- if (!Module.state || this.moveCount == 3) return;
- var kAB = (this.y2-this.y1)/((this.x2) -this.x1);
- var kBC = (this.y3-this.y2)/(this.x3- (this.x2));
- if (kAB == Infinity) kAB = 0;
- if (kBC == Infinity) kBC = 0;
- //贝塞尔曲线是否成直线
- if (parseInt(kAB) == parseInt(kBC)) {
- Module.state = false;
- return;
- }
- if (this.x3 == this.windowSize.width / 2) {
- this.x2 = this.windowSize.width / 2;
- Module.state = false;
- return;
- } else if (this.x2 < this.x3) {
- if (this.moveCount != 1 && this.moveCount < 3) ++this.moveCount;
- this.x2 += 8;
- } else {
- if (this.moveCount != 2 && this.moveCount < 3) this.moveCount+=2;
- this.x2 -= 8;
- }
- //绘制钓鱼线
- if (this.fishingLine) {
- this.fishingLine.clear();
- this.fishingLine.moveTo(this.x1, this.y1);
- this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
- this.fishingLine.stroke();
- Module.fishingLine = this.fishingLine;
- this.hook.x = this.x3- 14;
- this.limits.x = this.hook.x;
- this.setHookRotation();
- }
- },
- //还原鱼钩状态
- restore: function() {
- this.y1 = this.windowSize.height + 300;
- this.x1 = this.windowSize.width / 2;
-
- this.y2 = this.windowSize.height - this.windowSize.height/ 2 / 2 ;
- this.x2 = this.windowSize.width / 2;
- this.y3 = this.windowSize.height / 2 -50;
- this.x3 = this.windowSize.width / 2;
- this.hook.x = this.x3 - 14;
- this.hook.y = this.y3 + 3;
- var ctx = this.getComponent(cc.Graphics);
- if (!ctx) ctx = this.addComponent(cc.Graphics);
-
- ctx.clear();
- ctx.lineWidth = 2;
- ctx.strokeColor = cc.Color.BLACK;
- ctx.moveTo(this.x1, this.y1);
- ctx.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
- ctx.stroke();
-
- Module.fishingLine = this.fishingLine = ctx;
- this.hook.rotation = 0;
- this.limits.x = this.hook.x;
- },
- });
|