因此,我的角色正在由Xbox One控制器的左操纵杆控制的屏幕上移动,但是每当我将左操纵杆倾斜特定角度时,sdl_pollevent
都不会检测到任何事件,尽管我实际上仍在向左倾斜一直坚持下去我已经通过在writeln(random(10))
语句之后插入if sdl_pollevent(sdlevent)=1 then
来确认这一点,并且没有显示随机数,并且sdl_pollevent(sdlevent)
保持等于零。
我获得控制器操纵杆事件的代码部分是(请注意,条件语句中还有其他代码可以满足键盘和鼠标以及控制器按钮和触发事件的需要,但我在此处未包括):< / p>
if sdl_pollevent(sdlevent)=1 then
begin
if joystickuse=true then
begin
if sdlevent^.type_=sdl_joyaxismotion then
begin
joystickactive:=true;
if sdlevent^.jaxis.which=0 then
begin
{ writeln(sdlevent^.jaxis.axis,' ',sdlevent^.jaxis.value); }
if sdlevent^.jaxis.axis=0 then
begin
{ writeln('y ',sdlevent^.jaxis.value); }
joystickxy[2]:=sdlevent^.jaxis.value;
if sdlevent^.jaxis.value<-joystickdeadzone then joystickaxis[1,4]:=true; { left }
if sdlevent^.jaxis.value>joystickdeadzone then joystickaxis[1,3]:=true; { right }
if (sdlevent^.jaxis.value>=-joystickdeadzone) and (sdlevent^.jaxis.value<=joystickdeadzone) then
begin
joystickaxis[1,3]:=false;
joystickaxis[1,4]:=false;
joystickmenuscrollticks1[1]:=0;
joystickmenuscrollbool1:=false;
end
else
begin
if joystickmenuscrollticks1[1]=0 then joystickmenuscrollticks1[1]:=sdl_getticks;
end;
{ left stick y (- left, + right) }
end;
if sdlevent^.jaxis.axis=1 then
begin
{ writeln('x ',sdlevent^.jaxis.value); }
joystickxy[1]:=sdlevent^.jaxis.value;
if sdlevent^.jaxis.value<-joystickdeadzone then joystickaxis[1,2]:=true; { up }
if sdlevent^.jaxis.value>joystickdeadzone then joystickaxis[1,1]:=true; { down }
if (sdlevent^.jaxis.value>=-joystickdeadzone) and (sdlevent^.jaxis.value<=joystickdeadzone) then
begin
joystickaxis[1,1]:=false;
joystickaxis[1,2]:=false;
joystickmenuscrollticks[1]:=0;
joystickmenuscrollbool:=false;
end
else
begin
if joystickmenuscrollticks[1]=0 then joystickmenuscrollticks[1]:=sdl_getticks;
end;
{ left stick x (- up, + down) }
end;
if sdlevent^.jaxis.axis=2 then
begin
if sdlevent^.jaxis.value<-joystickdeadzone then joystickaxis[2,4]:=true; { left }
if sdlevent^.jaxis.value>joystickdeadzone then joystickaxis[2,3]:=true; { right }
if (sdlevent^.jaxis.value>=-joystickdeadzone) and (sdlevent^.jaxis.value<=joystickdeadzone) then
begin
joystickaxis[2,3]:=false;
joystickaxis[2,4]:=false;
joystickmenuscrollticks1[1]:=0;
joystickmenuscrollbool1:=false;
end
else
begin
if joystickmenuscrollticks1[1]=0 then joystickmenuscrollticks1[1]:=sdl_getticks;
end;
{ right stick y }
end;
if sdlevent^.jaxis.axis=3 then
begin
if sdlevent^.jaxis.value<-joystickdeadzone then joystickaxis[2,2]:=true; { up }
if sdlevent^.jaxis.value>joystickdeadzone then joystickaxis[2,1]:=true; { down }
if (sdlevent^.jaxis.value>=-joystickdeadzone) and (sdlevent^.jaxis.value<=joystickdeadzone) then
begin
joystickaxis[2,1]:=false;
joystickaxis[2,2]:=false;
joystickmenuscrollticks[1]:=0;
joystickmenuscrollbool:=false;
end
else
begin
if joystickmenuscrollticks[1]=0 then joystickmenuscrollticks[1]:=sdl_getticks;
end;
{ right stick x }
end;
if sdlevent^.jaxis.axis=4 then
begin
{ writeln(sdlevent^.jaxis.value); }
if (activemenum[1]=false) and (activemenum[2]=false) then
begin
if sdlevent^.jaxis.value>joysticktriggersdeadzone then
begin
joysticktriggers[1]:=true;
end
else
begin
joysticktriggers[1]:=false;
end;
end;
{ left trigger }
end;
if sdlevent^.jaxis.axis=5 then
begin
{ writeln(sdlevent^.jaxis.value); }
if sdlevent^.jaxis.value>joysticktriggersdeadzone then
begin
joysticktriggers[2]:=true;
end
else
begin
joysticktriggers[2]:=false;
end;
{ right trigger }
end;
joystickangle:=round(arctan2(joystickxy[2],joystickxy[1])*(180/pi)); { used for getting joystick angle: left stick bottom 0, right 90, top 180, left -90, top -180}
if (joystickxy[1]=0) and (joystickxy[2]=0) then joystickangle:=0;
{ writeln(joystickangle); }
o[1]:=0;
repeat
o[1]:=o[1]+1;
joystick8dirstate[1,o[1]]:=false;
joystick8dirstate[2,o[1]]:=false;
until o[1]=8;
if joystickaxis[1,1]=true then
begin
if (joystickangle>=-round(joystick8dirtolangle/2)) and (joystickangle<=0) or (joystickangle>=0) and (joystickangle<=round(joystick8dirtolangle/2)) then joystick8dirstate[1,1]:=true; { down }
end;
if joystickaxis[1,2]=true then
begin
if (joystickangle>=-180) and (joystickangle<=-180+round(joystick8dirtolangle/2)) or (joystickangle>=180-round(joystick8dirtolangle/2)) and (joystickangle<=180) then joystick8dirstate[1,2]:=true; { up }
end;
if (joystickaxis[1,3]=true) and (joystickangle>=90-joystick8dirtolangle) and (joystickangle<=90+joystick8dirtolangle) then joystick8dirstate[1,3]:=true; { right }
if (joystickaxis[1,4]=true) and (joystickangle>=-90-joystick8dirtolangle) and (joystickangle<=-90+joystick8dirtolangle) then joystick8dirstate[1,4]:=true; { left }
if (joystickaxis[1,2]=true) and (joystickaxis[1,3]=true) and (joystickangle>=90+45-joystick8dirtolangle) and (joystickangle<=90+45+joystick8dirtolangle) then
begin
joystick8dirstate[1,5]:=true; { up+right }
joystick8dirstate[1,2]:=false;
joystick8dirstate[1,3]:=false;
end;
if (joystickaxis[1,1]=true) and (joystickaxis[1,3]=true) and (joystickangle>=90-45-joystick8dirtolangle) and (joystickangle<=90-45+joystick8dirtolangle) then
begin
joystick8dirstate[1,6]:=true; { down+right }
joystick8dirstate[1,1]:=false;
joystick8dirstate[1,3]:=false;
end;
if (joystickaxis[1,2]=true) and (joystickaxis[1,4]=true) and (joystickangle>=-90-45-joystick8dirtolangle) and (joystickangle<=-90-45+joystick8dirtolangle) then
begin
joystick8dirstate[1,7]:=true; { up+left }
joystick8dirstate[1,2]:=false;
joystick8dirstate[1,4]:=false;
end;
if (joystickaxis[1,1]=true) and (joystickaxis[1,4]=true) and (joystickangle>=-90+45-joystick8dirtolangle) and (joystickangle<=-90+45+joystick8dirtolangle) then
begin
joystick8dirstate[1,8]:=true; { down+left }
joystick8dirstate[1,1]:=false;
joystick8dirstate[1,4]:=false;
end;
{ joystick8dirstate:array[1..2,1..8] of boolean; left/right joystick, down/up/right/left/up+right/down+right/up+left/down+left }
end;
end;
end;
end;
因此,我重新编码了此代码,使操纵杆值在死区之外时,仅使用以下命令即可移动角色: 如果sdlevent ^ .jaxis.value <-joystickdeadzone,则joystickaxis [1,2]:= true; {上} 如果sdlevent ^ .jaxis.value> joystickdeadzone,则joystickaxis [1,1]:= true; {下}
我之前尝试做的是用摇杆模拟8个方向的运动。我这样做是因为,例如,当角色向右移动时,稍微向上或向下移动操纵杆都会导致操纵杆也朝那个方向移动。我想摆脱那是多么敏感。但是由于某种原因,事件停止了轮询。