The previous blog to achieve a fixed rocker, in the market many games have applications, but in the end also put forward a not too good place, that is, to move the position is relatively far away, the operation is more tired, in order to solve this problem, there are follow-style rocker. What is a follower rocker
The so-called follower rocker, that is: when the rocker center in the rocker Circle range, as the fixed type, the big circle does not move, the center moves, when the remote sensing center moves to the circle outside, the circle and center remain relative position unchanged, great circle move. Two position calculation
The computation of movement within the great circle is the same as that of the previous one, mainly looking at the computational process beyond.
The position of the joystick in the touch:
The position after the move is as follows:
The abstraction is:
three examples
First, modify the previous project and drag the Sprokercenter from the Sproker, as shown in the figure:
Then replace the above calculation procedure with code:
Cc. Class ({extends:cc.Component, properties: {spplayer:cc). Sprite, SPROKER:CC. Sprite, SPROKERCENTER:CC. Sprite, Movespeed: {type:cc. Float, default:1}, Maxradius: {type:cc. Float, default:100}}, Onload:function () {This.orirokerpos = this.sproker.node.g
Etposition ();
This.orirokercenterpos = This.spRokerCenter.node.getPosition (); This.spRoker.node.on (CC.
Node.EventType.TOUCH_START, This.ontouchstart, this); This.spRoker.node.on (CC.
Node.EventType.TOUCH_MOVE, This.ontouchmove, this); This.spRoker.node.on (CC.
Node.EventType.TOUCH_END, This.ontouchend, this); This.spRoker.node.on (CC.
Node.EventType.TOUCH_CANCEL, This.ontouchcancel, this);
}, Ontouchstart:function (event) {var touchpos = event.getlocation ();
var pos = This.spRoker.node.parent.convertToNodeSpaceAR (Touchpos); var dir = This.getdirection (POS);
This.movedir = This.getdirection (POS);
This.updaterokerpos (POS);
}, Ontouchmove:function (event) {var touchpos = event.getlocation ();
var pos = This.spRoker.node.parent.convertToNodeSpaceAR (Touchpos);
This.movedir = This.getdirection (POS);
This.updaterokerpos (POS);
}, Ontouchend:function (event) {this.spRokerCenter.node.setPosition (this.orirokercenterpos);
This.spRoker.node.setPosition (This.orirokerpos);
This.movedir = null;
}, Ontouchcancel:function (event) {this.spRokerCenter.node.setPosition (this.orirokercenterpos);
This.spRoker.node.setPosition (This.orirokerpos);
This.movedir = null;
}, Getdirection:function (POS) {var oripos = this.spRoker.node.getPosition (); var rad = math.atan2 (Pos.y-oripos.y, pos.x-oripos.x);//[-pi, PI] if (rad >=-math.pi/8 && Rad & Lt 0) | | (rad >= 0 && rad < MATH.PI/8)) {return cc.v2 (1, 0);/right} else if (rad >= math.pi/8 && R Ad < 3 * math.pi/8) {return cc.v2 (1, 1);//Upper Right} else if (rad >= 3 * math.pi/8 && RA D < 5 * math.pi/8) {return cc.v2 (0, 1);//on} else if (rad >= 5 * MATH.PI/8 && rad < 7 * MATH.PI/8) {return cc.v2 (-1, 1);//left Upper} else if (rad >= 7 * MATH.PI/8 && rad < Math.PI) | | (Rad >=-math.pi && rad <-7 * MATH.PI/8)) {return Cc.v2 (-1, 0);//left} else if (rad >=-7 * MATH.PI/8 && rad <-5 * MATH.PI/8) {return Cc.v2 (-1,-1);//lower left} else if (RAD >=-5 * MATH.PI/8 && rad <-3 * Math.PI/ 8) {return cc.v2 (0,-1);/else {return Cc.v2 (1,-1); Daterokerpos:function (POS) {This.spRokerCenter.node.setPosition (POS);
var oripos = this.spRoker.node.getPosition ();
var Subvec = cc.v2 (pos.x-oripos.x, POS.Y-ORIPOS.Y);
var len = Subvec.mag ();
if (Len > This.maxradius) {var rate = This.maxradius/len;
var x = pos.x + (oripos.x-pos.x) * rate;
var y = pos.y + (ORIPOS.Y-POS.Y) * rate;
This.spRoker.node.setPosition (x, y);
}, Updateplayerpos:function (dir) {var size = cc.director.getWinSize ();
var x = this.spplayer.node.x + dir.x * this.movespeed;
var MaxX = size.width * 0.5-this.spplayer.node.width * 0.5; var x = x > 0?
Math.min (x, MaxX): Math.max (x,-maxx);
var y = this.spplayer.node.y + dir.y * this.movespeed;
var Maxy = size.height * 0.5-this.spplayer.node.height * 0.5; var y = y > 0?
Math.min (Y, Maxy): Math.max (y,-maxy);
This.spPlayer.node.setPosition (x, y); }, Update:function (dt) {if (This.movedir) {this.updateplayerpos (this.movedir);
}
},
});
This solves the previous blog last put forward, when the finger needs to move a long distance problem, follow the type to only need to move a maxradius, can complete the steering.
The current market game is mainly the two modes of operation.