rectangle command not working in a function with multiple variables in the coordinate argument

1 次查看(过去 30 天)
This has stumped me for a while now and it's very annoying.
this is my function, it uses the KeyPressFcn to move a rectangle up, down, left and right with the arrow keys:
figure('Color',[0.1 .6 0],'Name',...
'Highwayman','MenuBar','none','NumberTitle','off','Resize','off',...
'position',[500,100,400,800],'KeyPressFcn',@userkey);
axis([0,5,0,20])
set(gca,'color','black')
driverx = 2;
drivery = 1;
startingcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
function userkey(src,event)
global driverx drivery redcar startingcar
k = event.Key;
%disp(event.Key)
switch k
case 'leftarrow'
driverx = driverx-1;
delete(redcar)
if driverx<0
driverx = 0;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'rightarrow'
driverx = driverx+1;
delete(redcar)
if driverx>4
driverx = 4;
end
%rightturn = [driverx+.25 drivery .5 2];
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'uparrow'
drivery = drivery+2;
delete(redcar)
if driverx>18
driverx = 18;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'downarrow'
drivery = drivery-2;
delete(redcar)
if driverx<1
driverx = 1;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
end
end
Whenever the user presses a key it should move the car in that direction, with if statements preventing it from leaving the border, and indeed when it is just left and right, where drivery is replaced with just the number 1 in the rectangle command, it works fine, but when I add the second dimension of up and down, it gives an error when I try to move in any direction:
Error using rectangle
Value must be a 4 element vector
Error in Highwayman>userkey (line 143)
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
Error using Highwayman (line 103)
Error while evaluating Figure KeyPressFcn.
It's referring to [driverx+.25 drivery .5 2] because it works fine when it's just left and right: [driverx+.25 1 .5 2]. The value is definitely a 4 element vector, it looking like [2.25 1 0.5 2] if assigned to a variable and the variable put in its place but it returns the same error and doesn't move the rectangle for any input.
The rectangle function with two variables works fine outside that function and in the command window, it's just there where it doesn't work. Any help?

采纳的回答

Walter Roberson
Walter Roberson 2021-12-5
driverx = 2;
drivery = 1;
You do not declare those as global in the function you make the assignments in.
global driverx drivery redcar startingcar
Because you did not initialize those after they were declared global, those will be empty.
case 'rightarrow'
driverx = driverx+1;
delete(redcar)
if driverx>4
driverx = 4;
end
In that code, you adjust the value of driverx by 1. Then you test whether driverx exceeds a particular value and if so change it.
case 'uparrow'
drivery = drivery+2;
delete(redcar)
if driverx>18
driverx = 18;
end
In that code, you adjust the value of drivery by 2. Then you test whether driverx exceeds a certain value, and if so change it.
Why are you asymmetric in the cases? Left and right, you adjust driverx and then test driverx, but for up and down, you adjust drivery and then test driverx ??
  1 个评论
Nathaniel Stringer
Nathaniel Stringer 2021-12-5
Thank you, the driverx on uparrow and downarrow was an oversight by me, and making those variables global in the original function worked, despite not being needed when it's just one variable or the other being used.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by