我和我的队友正在用飞镖手机浏览器编写游戏“Battlecity”。问题是在移动浏览器中滑动坦克方向(从左到右,从右到左等)。它有很大的作用(对于某些reasond在Chrome浏览器中不起作用)。 如果您在移动设备上启动游戏,则可以更好地查看问题(只需打开游戏链接:https://javajunikorn.github.io/BattleCity/build/web/html/test.html) 有趣的是,问题只存在于游戏领域的滑动上(在我们的例子中它是27 * 27 html表)。如果我们从游戏中解脱出来,那就好了。
以下是我们的控制器代码:
class Direction{
Point first, last;
Game game;
void startListening(){
window.onTouchStart.listen((ev) {
last = null;
first = ev.touches.first.client;
});
window.onTouchMove.listen((ev){
last = ev.touches.first.client;
});
window.onTouchEnd.listen((ev) {
if(last == null || first.distanceTo(last) < 20)
game.player.shoot();
else {
Point d = first - last;
if(d.x.abs() > d.y.abs()){
//Waagerecht
if( first.x > last.x){
//links
game.player.changeDirection(Directions.left);
}
else{
//rechts
game.player.changeDirection(Directions.right);
}
}
else{
//Senkrecht
if(first.y > last.y){
//Up
game.player.changeDirection(Directions.up);
}
else{
//Down
game.player.changeDirection(Directions.down);
}
}
}
});
window.onKeyPress.listen((k) {
//Shoot
if(k.which == 32) { //spacebar
game.player.shoot();
}
//Up
if(k.which == 119 || k.keyCode == KeyCode.UP){
game.player.changeDirection(Directions.up);
}
//Down
if(k.which == 115 || k.keyCode == KeyCode.DOWN){
game.player.changeDirection(Directions.down);
}
//Left
if(k.which == 97 || k.keyCode == KeyCode.LEFT){
game.player.changeDirection(Directions.left);
}
//Right
if(k.which == 100 || k.keyCode == KeyCode.RIGHT){
game.player.changeDirection(Directions.right);
}
});
}
}