Skip to content

Commit d8cbbdd

Browse files
committed
improve positioning for the mouse
1 parent 0773925 commit d8cbbdd

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

04-Breakout-Game/script.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,20 @@ function keyRight() {// pressed right arrow key
316316
}
317317
//增加一个触摸屏幕事件,让Paddle水平移动距离和触摸点水平移动距离一致
318318
var touchXstart = null;
319+
var previousPosition = canvas.width/2-paddle.w/2;
319320
function actionStart(e){
320321
e.preventDefault();
321322
//判断是鼠标左键点击还是触摸
322-
323323
if(e.touches && touchXstart == null){//如果是触摸 touchXstart等于第一个touch点的x坐标
324324
touchXstart = e.touches[0].pageX;
325325
}else{//如果是鼠标 touchXstart等于鼠标的x坐标
326326
touchXstart = e.pageX;
327327
}
328-
329-
330328
}
331329
function actionEnd(e){
332330
e.preventDefault();
333331
touchXstart = null;
332+
previousPosition = paddle.x;
334333
}
335334
function actionMove(e){
336335
//e.preventDefault();
@@ -343,10 +342,9 @@ function actionMove(e){
343342
dist = e.pageX - touchXstart;
344343
}
345344
// dist 按照画布的放大比例缩放
346-
var canvasS = document.getElementById("canvas");
347-
dist = dist / canvas.width *canvasS.clientWidth /2;
345+
348346

349-
paddle.x = paddle.x + dist;
347+
paddle.x = previousPosition + dist;
350348
if (paddle.x + paddle.w > canvas.width) paddle.x = canvas.width - paddle.w;
351349
if (paddle.x < 0) paddle.x = 0;
352350
}

0 commit comments

Comments
 (0)