Skip to content

Commit 425087d

Browse files
committed
- Bug fix for clearing canvas for joysticks
1 parent 0bab6ce commit 425087d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

gamecontroller.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,27 @@
258258
boundingSet: function( options ) {
259259
var directions = ['left', 'right'];
260260

261-
var width = options.width || ( ( this.getPixels( options.radius ) + this.getPixels( options.stroke ) ) * 2 );
262-
var height = options.height || ( ( this.getPixels( options.radius ) + this.getPixels( options.stroke ) ) * 2 );
263-
264-
var left = this.getPixels( options.x );
265-
var right = this.getPixels( options.x ) + this.getPixels( width );
266-
var top = this.getPixels( options.y );
267-
var bottom = this.getPixels( options.y + height );
261+
// Square - pivot is top left
262+
if( options.width )
263+
{
264+
var width = this.getPixels( options.width );
265+
var height = this.getPixels( options.height );
266+
var left = this.getPixels( options.x );
267+
var top = this.getPixels( options.y );
268+
}
269+
// Circle - pivot is center
270+
else
271+
{
272+
if( this.options.touchRadius )
273+
var radius = this.getPixels( options.radius ) * 2 + ( this.getPixels( this.options.touchRadius ) / 2 ); // size of the box the joystick can go to
274+
else
275+
var radius = options.radius;
276+
var width = height = ( radius + this.getPixels( options.stroke ) ) * 2;
277+
var left = this.getPixels( options.x ) - ( width / 2 );
278+
var top = this.getPixels( options.y ) - ( height / 2 );
279+
}
280+
var right = left + width;
281+
var bottom = top + height;
268282

269283
if( this.bound.left === false || left < this.bound.left )
270284
this.bound.left = left;
@@ -1117,8 +1131,8 @@
11171131
*/
11181132
TouchableJoystick.prototype.check = function( touchX, touchY ) {
11191133
if(
1120-
( Math.abs( touchX - this.x ) < this.radius + ( GameController.options.touchRadius / 2 ) ) &&
1121-
( Math.abs( touchY - this.y ) < this.radius + ( GameController.options.touchRadius / 2 ) )
1134+
( Math.abs( touchX - this.x ) < this.radius + ( GameController.getPixels( GameController.options.touchRadius ) / 2 ) ) &&
1135+
( Math.abs( touchY - this.y ) < this.radius + ( GameController.getPixels( GameController.options.touchRadius ) / 2 ) )
11221136
)
11231137
return true;
11241138

@@ -1172,7 +1186,7 @@
11721186
{
11731187
var subCanvas = document.createElement( 'canvas' );
11741188
this.stroke = this.stroke || 2;
1175-
subCanvas.width = subCanvas.height = 2 * ( this.radius + this.stroke );
1189+
subCanvas.width = subCanvas.height = 2 * ( this.radius + ( GameController.options.touchRadius ) + this.stroke );
11761190

11771191
var ctx = subCanvas.getContext( '2d' );
11781192
ctx.lineWidth = this.stroke;

0 commit comments

Comments
 (0)