KeyPressFcn problem, Undefined function or variable

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

Use nested functions and shared variables.
function outer
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 %end of keyDown
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 %end of keyUp
end %end of outer
However... changing Vz and Vx have no effect on the ball position in the display. In order to change the position in the display, you need to do one of:
  • erase the current point plot and draw a new plot; or
  • update the XData and YData properties of the existing graphics object
I am not clear as to what your keyUp is intended to do. My expectation for a system like yours would be that each press would move the ball one unit it a particular direction, and leave it there. The stuff you are doing with keyUp implies you are resetting the position upon releasing the key. Which might make sense, possibly, if your system were generating auto-repeat "down" events without any "up" event until you released the key: in such a situation they ball would move in some direction until you released, and then would jump back. Doesn't sound as useful to me as having the down move the ball and leave it moved... which is something that would not need a Key Up processing, provided that you fetch the old position, modify it according to the current key, and set the new position accordingly.

3 Comments

omg! I was almost giving up hope. It was a pity to forgo this project without knowing for certain what the problem was. Thanks to you, I found my fault, and I knew what direction to take.
Thank you. I'll keep studying!!
Oh yes, another thing:
set(figure(1),'KeyPressFcn',@keyDown,'KeyPressFcn', @keyUp)
You cannot set the same property twice. You need
set(figure(1),'KeyPressFcn',@keyDown,'KeyReleaseFcn', @keyUp)
This is what I wanted.
I'll have to work on it a little more, but I'm so glad I'm taking a step closer to completion.
sss;
function sss
V_x=[];
V_z=[];
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)
function keyDown (~,event)
switch event.Key
case 'w'
V_z = V_z+1;
case 'a'
V_x = V_x-1;
case 's'
V_z = V_z-1;
case 'd'
V_x= V_x+1;
end
ball_x = 29 + V_x;
ball_y = 0;
ball_z = 29 + V_z;
plot3(ball_x,ball_y,ball_z,'ok');
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!