背景: 我目前正在将游戏移植到Android上,但我实际上没有任何有关如何制作适当的触摸操纵杆的好的文档。原始游戏使用的鼠标可以在8个方向上移动角色,因此N,S,W,E和NE,NW,SE,SW。
我正在尝试什么: 我正在尝试使用SDL_MOUSEBUTTONDOWN然后使用SDL_MOUSEMOTION来获取X和Y坐标。在SDL_MOUSEBUTTONDOWN上,我保存X,Y坐标,在SDL_MOUSEMOTION上,我试图求出差值,以计算用户移动手指的方向,并据此决定如何移动字符。
问题: 命中SDL事件SDL_MOUSEMOTION时,X轴和Y轴可能相差10个左右。因此,如果我向下滑动,Y轴应该增加,我注意到X轴的变化会太小。这给制作精确的操纵杆带来了一些困难。
如何正确制作触摸板操纵杆?这是正确的方法吗?我如何进行对角线运动而不会弄乱其余运动?
示例代码:
int Xbase = 0;
int Ybase = 0;
SDL_Event e;
switch (e.type){
case SDL_MOUSEMOTION:
SDL_Log("MOVING MOUSE X %d Y %d \n", e.motion.x , e.motion.y);
if (Ybase != 0 && Xbase != 0 ){
if( (Ybase) < e.motion.y && (e.motion.y - Ybase >= 10) ){
SDL_Log("WALKING SOUTH\n");
walkInDir(WALK_S);
}
if( (Ybase) > e.motion.y && (Ybase - e.motion.y >= 10)){
SDL_Log("WALKING NORTH\n");
walkInDir(WALK_N);
}
if( (Xbase) > e.motion.x && (Xbase - e.motion.x >= 10)){
SDL_Log("WALKING WEST\n");
walkInDir(WALK_W);
}
if( (Xbase) < e.motion.x && ( e.motion.x - Xbase >= 10)){ // ( (e.motion.x - Xbase) >= 10)
SDL_Log("WALKING EAST\n");
walkInDir(WALK_E);
}
// I need to add in NE, NW , SE , SW
}break;
case SDL_MOUSEBUTTONDOWN: {
int button = e.button.button;
if (button == SDL_BUTTON_LEFT) {
SDL_Log("BUTTON DOWN X %d Y %d \n", e.button.x , e.button.y);
Xbase = e.button.x;
Ybase = e.button.y;
}
}break;
case SDL_MOUSEBUTTONUP: {
int button = e.button.button;
if (button == SDL_BUTTON_LEFT) {
Xbase = 0;
Ybase = 0;
}
}
}