KeyPressFcn problem, Undefined function or variable
Show older comments
I want the ball to change position by one when I press the key.
but a variable in a function is defined as separate from a variable outside the function.
I am trying to solve this problem almost two weeks .Please help me.
V_x=0;
V_z=0;
ball_x = 29 + V_x;
ball_y = 0;
ball_z = 29 + V_z;
plot3(ball_x,ball_y,ball_z,'ok')
set(figure(1),'KeyPressFcn',@keyDown,'KeyPressFcn', @keyUp)
function keyDown (~,event)
switch event.Key
case 'w'
V_z = +1;
case 'a'
V_x = -1;
case 's'
V_z = -1;
case 'd'
V_x= +1;
end
end
function keyUp(~,event)
switch event.Key
case 'w'
if V_z == 1
V_z = 0;
end
case 'a'
if V_x == -1
V_x = 0;
end
case 's'
if V_z == 1
V_z = 0;
end
case 'd'
if V_x == -1
V_x = 0;
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Clocks and Timers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!